8

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

注意:
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>
4

4 回答 4

11

Twitter 不接受 OAuth 请求(Twitter API 公告)中请求的回调,只会重定向到应用程序设置中指定的回调 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=...(所需的结果)

于 2010-03-08T12:11:40.057 回答
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

于 2011-08-23T13:55:26.443 回答
0

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

于 2010-02-04T15:56:59.033 回答
0

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

于 2013-02-13T11:12:34.947 回答