首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓OAuth +推特:回调失败

安卓OAuth +推特:回调失败
EN

Stack Overflow用户
提问于 2010-02-04 19:51:06
回答 4查看 13.7K关注 0票数 8

我的安卓应用程序使用Java OAuth库,在Twitter上找到了授权的here。我可以得到一个请求令牌,授权令牌和确认,但是当浏览器尝试回调url来重新连接我的应用程序时,它不会使用我在代码中提供的URL,而是使用我在Twitter注册时提供的URL。

注意:

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

你知道我做错了什么吗?

相关代码:

代码语言:javascript
运行
复制
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>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-03-08 20:11:40

推特不接受OAuth请求(Twitter API Announce)中请求的回调,只会重定向到应用程序设置中指定的回调URL (请注意,"localhost“是不允许的)。

我假设你检查了Oauth-callback-on-android问题。

Android猜想--

读了一下,我发现Android浏览器将MyApp:/重定向到你的应用程序,我猜Twitter不喜欢这个定制的URI前缀。我不是安卓开发人员,但我可能会提出一个建议,那就是把"www.myapp.com“放到网上,然后重定向到那里。

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

票数 11
EN

Stack Overflow用户

发布于 2011-08-23 21:55:26

在我的例子中,我做到了这一点:

代码语言:javascript
运行
复制
 String authURL = m_provider.retrieveRequestToken (m_consumer, CALLBACK_URL);

在清单中:

代码语言:javascript
运行
复制
    <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

票数 3
EN

Stack Overflow用户

发布于 2010-02-04 23:56:59

在我看来,你做的是正确的事情,而Twitter总是接受你注册的回调URL,这就搞砸了。有没有办法更改已注册的URL?也许你可以重新注册,下次尝试Android回调,看看会发生什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2199357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档