Android 上的 OAuth + Twitter:回调失败 [英] OAuth + Twitter on Android: Callback fails

查看:17
本文介绍了Android 上的 OAuth + Twitter:回调失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My Android application uses Java OAuth library, found here for authorization on Twitter. I am able to get a request token, authorize the token and get an acknowlegement but when the browser tries the call back url to reconnect with my application, it does not use the URL I provide in code, but uses the one I supplied while registering with Twitter.

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

Note:
1. When registering my application with twitter, I provided a hypothetical call back url:http://abz.xyc.com and set the application type as browser.
2. I provided a callback url in my code "myapp" and have added an intent filter for my activity with Browsable category and data scheme as "myapp".
3. URL called when authorizing does contain te callback url, I specified in code.

知道我在这里做错了什么吗?

Any idea what I am doing wrong here?

相关代码:

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>

推荐答案

Twitter 不接受 OAuth 请求中请求的回调 (Twitter API 公告),并且只会重定向到应用程序设置中指定的回调 URL(注意不允许使用localhost").

Twitter does not honor callbacks requested in OAuth requests (Twitter API Announce) and will only redirect to the callback URL specified in the Application Settings (note that "localhost" is not allowed).

我假设您检查了 Oauth-callback-on-android 问题.

I assume you checked Oauth-callback-on-android question.

Android 猜测--
稍微阅读之后,我看到 Android 浏览器将 MyApp:///重定向到您的应用程序,我猜 Twitter 不喜欢这个定制的 URI 前缀.我不是 android 开发人员,但我可能提出的一个建议是在网络上获取www.myapp.com"并在那里重新重定向.

Android guesswork--
After a bit of reading up, I see Android browser redirects MyApp:/// to your application and I'm guessing Twitter doesn't like this bespoke URI prefix. I'm no android developer but one suggestion I might make is to get "www.myapp.com" on the web and have a re-redirect there.

因此,让您的 OAuth 返回到 http://www.myapp.com/redirect.aspx?oauth_token=abc 并将该页面重定向到 myapp:///oauth_token=...(想要的结果)

So have your OAuth return to http://www.myapp.com/redirect.aspx?oauth_token=abc and have that page redirect to myapp:///oauth_token=... (the desired result)

这篇关于Android 上的 OAuth + Twitter:回调失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆