【问题标题】:OAuth + Twitter on Android: Callback failsAndroid 上的 OAuth + Twitter:回调失败
【发布时间】:2011-01-13 01:12:09
【问题描述】:

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

注意:
1.在twitter注册我的应用程序时,我提供了一个假设的回调url:http://abz.xyc.com,并将应用程序类型设置为浏览器。
2. 我在代码“myapp”中提供了一个回调 url,并为我的活动添加了一个意图过滤器,其中可浏览类别和数据方案为“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>

【问题讨论】:

    标签: android twitter oauth twitter-client


    【解决方案1】:

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

    【讨论】:

    • 是的,我们可以通过单击编辑应用程序设置按钮来更改注册的 URL。我尝试了两组不同的 URL,但运气不佳,也许我会重新注册,看看会发生什么。
    【解决方案2】:

    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. 是的,我确实检查了 Oauth-callback-on-Android 问题并相应地设置了我的应用程序类型(即浏览器)。再次感谢。
    • 添加了指向 Twitter API 公告的链接。
    • 如果在 OAuth 请求中请求的回调未得到遵守,那么我如何在授权后回调我的应用程序? Twitter 不接受 Android 风格的 URL,这些需要被覆盖。如果上述方法不起作用,还有哪些替代方法?请帮忙。 [你现在得到10分;回答完最后一个问题后 125 :P]
    • 感谢您的时间,伊恩;将尝试弄清楚我们如何做到这一点。当然,我不会一个人在这里......再次感谢。
    【解决方案3】:

    就我而言,我有这个工作:

     String authURL = m_provider.retrieveRequestToken (m_consumer, CALLBACK_URL);
    

    在清单中:

        <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
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2011-12-09
      • 1970-01-01
      • 2012-12-15
      • 2013-02-05
      • 2011-08-07
      • 2021-09-18
      • 2011-03-13
      • 2013-10-22
      • 1970-01-01
      相关资源
      最近更新 更多