OAuth + Android 上的 Twitter:回调失败

发布于 2024-08-19 21:56:06 字数 3120 浏览 5 评论 0原文

我的 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海未深 2024-08-26 21:56:06

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=...< /code> (期望的结果)

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)

沙沙粒小 2024-08-26 21:56:06

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

 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>

在这种情况下,回调网址将是:myapp://tweet

In my case, i have this working:

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

And in the Manifest:

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

In this case the callback url will be: myapp://tweet

傲鸠 2024-08-26 21:56:06

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

It looks to me like you're doing the correct thing and Twitter is screwing up by always accepting your registered callback URL. Is there any way to change the registered URL? Maybe you could re-register and try an Android callback next time, see what happens.

怪异←思 2024-08-26 21:56:06

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

My problem was that I was trying to log in with the same account I made the Twitter app. After I logged in with my personal profile the call back works (so far).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文