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

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

问题描述

我的Andr​​oid应用程序使用Java OAuth的图书馆,发现这里授权的推特。我能得到一个请求令牌,授权令牌,并得到一个acknowlegement但是当浏览器尝试回调URL重新与我的应用程序,它不使用我提供code中的网址,但使用了一个我而与Twitter注册提供的。

注:
1.当我的注册与Twitter的应用程序,我提供了一个假设的回调网址:HTTP://abz.xyc.com和设置应用程序类型的浏览器
。 2.我在code的myapp提供了一个回调URL,并增加了一个意图过滤器我有可浏览的类别和数据方案为MyApp的活动。
授权调用时3. URL不会包含TE回调URL,我在code指定。

知不知道我做错了吗?

相关code:

 公共类FirstActivity扩展活动
{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        OAuthAccessor客户= defaultClient();
        意图I =新的意图(Intent.ACTION_VIEW);
        i.setData(Uri.parse(client.consumer.serviceProvider.userAuthorizationURL +?oauth_token =
                + client.requestToken +与& oauth_callback =+ client.consumer.callbackURL));

        startActivity(ⅰ);

    }

    OAuthServiceProvider defaultProvider()
    {
        返回新OAuthServiceProvider(GeneralRuntimeConstants.request_token_URL,
                GeneralRuntimeConstants.authorize_url,GeneralRuntimeConstants.access_token_url);
    }

    OAuthAccessor ​​defaultClient()
    {
        字符串callbackUrl =的myapp:///;
        OAuthServiceProvider提供商= defaultProvider();
        OAuthConsumer消费=新OAuthConsumer(callbackUrl,
                GeneralRuntimeConstants.consumer_key,GeneralRuntimeConstants.consumer_secret,
                供应商);
        OAuthAccessor访问=新OAuthAccessor(消费者);

        OAuthClient客户=新OAuthClient(新HttpClient4());
        尝试
        {
            client.getRequestToken(访问);
        }赶上(例外五)
        {
            e.printStackTrace();
        }

        返回访问;
    }

    @覆盖
    保护无效onResume()
    {
        // TODO自动生成方法存根
        super.onResume();

        。URI URI = this.getIntent()的getData();
        如果(URI!= NULL)
        {
            串access_token = uri.getQueryParameter(oauth_token);
        }
    }

}
//清单文件
 <应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
        <活动机器人:名称=。FirstActivity
                  机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
             <意向滤光器>
                <作用机器人:名称=android.intent.action.VIEW/>
                <类机器人:名称=android.intent.category.DEFAULT/>
                <类机器人:名称=android.intent.category.BROWSABLE/>
                <数据机器人:计划=MyApp的/>
            &所述; /意图滤光器>
        < /活性GT;
 < /用途>
 

解决方案

微博不接受在OAuth的请求,请求回调(<一href="http://groups.google.com/group/twitter-api-announce/browse_thread/thread/2086c8c5a12242ec/f7d06f563f022be9">Twitter API公布),只会重定向到应用程序设置中指定的回调URL(请注意,本地主机是不允许的)。

我想你检查<一href="http://stackoverflow.com/questions/1121329/oauth-callback-on-android">Oauth-callback-on-android的问题。

Android的猜测 -
经过一番的阅读了,我看到的Andr​​oid浏览器重定向的MyApp:///到你的应用程序,我猜的Twitter不喜欢这个定制的URI preFIX。我不是Android开发者,但一个建议我可能会是让www.myapp.com在网络上,有一个重新重定向那里。

所以,有你的OAuth返回 http://www.myapp.com/redirect.aspx?oauth_token=abc 并具有网页重定向到的myapp:/// oauth_token = ... (期望的结果)

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.

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?

Relevant Code:

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 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).

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

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.

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)

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

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