在Android上使用OAuth和Twitter:回调失败

9

我的Android应用程序使用Java OAuth库进行Twitter授权,可以在这里找到。我能够获得请求令牌,授权令牌并获得确认,但是当浏览器尝试使用回调URL重新连接我的应用程序时,它不使用我在代码中提供的URL,而是使用我在Twitter注册时提供的URL。

注意:
1. 在向Twitter注册我的应用程序时,我提供了一个假设的回调URL:http://abz.xyc.com,并将应用程序类型设置为浏览器。
2. 我在代码中提供了一个回调URL“myapp”,并为我的活动添加了可浏览类别和数据方案为“myapp”的意图过滤器。
3. 授权时调用的URL包含我在代码中指定的回调URL。

您有什么想法吗?

相关代码:

public class FirstActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        OAuthAccessor client = defaultClient();
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(client.consumer.serviceProvider.userAuthorizationURL + "?oauth_token="
                + client.requestToken + "&oauth_callback=" + client.consumer.callbackURL));

        startActivity(i);

    }

    OAuthServiceProvider defaultProvider()
    {
        return new OAuthServiceProvider(GeneralRuntimeConstants.request_token_URL,
                GeneralRuntimeConstants.authorize_url, GeneralRuntimeConstants.access_token_url);
    }

    OAuthAccessor defaultClient()
    {
        String callbackUrl = "myapp:///";
        OAuthServiceProvider provider = defaultProvider();
        OAuthConsumer consumer = new OAuthConsumer(callbackUrl,
                GeneralRuntimeConstants.consumer_key, GeneralRuntimeConstants.consumer_secret,
                provider);
        OAuthAccessor accessor = new OAuthAccessor(consumer);

        OAuthClient client = new OAuthClient(new HttpClient4());
        try
        {
            client.getRequestToken(accessor);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        return accessor;
    }

    @Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();

        Uri uri = this.getIntent().getData();
        if (uri != null)
        {
            String access_token = uri.getQueryParameter("oauth_token");
        }
    }

}
// Manifest file
 <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".FirstActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
             <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="myapp"/>
            </intent-filter>
        </activity>
 </application>
4个回答

11

Twitter在OAuth请求中不支持回调请求(Twitter API Announce),并且只会重定向到应用程序设置中指定的回调URL(注意,“localhost”不允许)。

我猜你看过了Oauth-callback-on-android这个问题。

Android 的猜想--
经过一番阅读后,我发现 Android 浏览器将 MyApp:/// 重定向到您的应用程序,而我猜 Twitter 不喜欢这个特定的URI前缀。虽然我不是 Android 开发人员,但我可能会提出一个建议,那就是让您的网站上线,比如"www.myapp.com",然后进行重定向。

所以,请让您的OAuth返回到http://www.myapp.com/redirect.aspx?oauth_token=abc,然后再将该页面重定向到myapp:///oauth_token=...(期望的结果)


谢谢您的回复。
  1. 您能否引用一下您所说的声明的参考文献: “Twitter不会遵守在OAuth请求中请求的回调,并且只会重定向到应用程序设置中指定的回调URL(请注意,“localhost”不允许)。”
  2. 是的,我已经检查了关于Android上的Oauth-callback问题,并相应地设置了我的应用程序类型(即浏览器)。
再次感谢。
- Samuh
添加了推特 API 公告的链接。 - Dead account
如果OAuth请求中未能遵守回调,则如何在授权后调用我的应用程序?Twitter不接受Android样式的URL,这些需要被覆盖。如果上述方法不起作用,有哪些替代方案?请帮忙。 - Samuh
谢谢你的时间,Ian;我会尝试弄清楚如何做到这一点。当然,在这里我不会孤单...再次感谢。 - Samuh

3
在我的情况下,我已经做到了这一点:
 String authURL = m_provider.retrieveRequestToken (m_consumer, CALLBACK_URL);

并在 Manifest 文件中:

    <activity android:configChanges = "keyboardHidden|orientation" android:name = "xxxx.android.xxxxx"> 
        <intent-filter>
            <action android:name   = "android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="myapp" android:host="tweet" />
        </intent-filter>

在这种情况下,回调URL将是:myapp://tweet

请问myapp是什么应用程序名称?在tweet的位置上要写什么? - Akanksha Rathore

0

在我看来,你正在做正确的事情,Twitter总是接受你注册的回调URL,这是他们的问题。有没有办法更改注册的URL?也许你可以重新注册并尝试使用Android回调,看看会发生什么。


是的,我们可以单击“编辑应用程序设置”按钮更改注册的URL。 我尝试了两组不同的URL,但没有太大的运气,也许我会重新注册并查看会发生什么。 - Samuh

0

我的问题是我试图使用创建Twitter应用程序的相同帐户登录。在我使用个人资料登录后,回调起作用了(到目前为止)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接