【问题标题】:How I can call back in Android using OAuth for Twitter?如何在 Android 中使用 OAuth for Twitter 进行回调?
【发布时间】:2011-06-06 08:47:07
【问题描述】:

从过去 5 天开始,我正在使用 OAuth 在 android 中为 twitter 寻找最佳工作代码……我发现了很多。但没有一个 1 运行完美。任何我如何获得一些代码。它的工作,但我有一个问题。我想知道我必须在 String callBack = "?" 中写什么以便我的 android 浏览器将我重定向回我的应用程序,而不是在身份验证后留在那里。到我的应用程序。请帮助我让我知道我做错了什么。

我的代码在这里

package com.example.tweeter;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class Tweeter extends Activity {

    private String CONSUMER_KEY = "CONSUMER_KEY";
    private String CONSUMER_SECRET = "CONSUMER_SECRET";
    private static final Uri CALLBACK_URI = Uri.parse("PicPuzzle://tkxel");
    private String CALLBACK_URL = "PicPuzzle://tkxel";
    OAuthProvider provider ;
    CommonsHttpOAuthConsumer consumer ;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        consumer = new CommonsHttpOAuthConsumer(
                CONSUMER_KEY, CONSUMER_SECRET);

        provider = new CommonsHttpOAuthProvider(
                "http://twitter.com/oauth/request_token",
                "http://twitter.com/oauth/access_token",
                "http://twitter.com/oauth/authorize");
        provider.setOAuth10a(true);

        HttpClient client = new DefaultHttpClient();

        String authUrl = "http://www.yahoo.com";
        try {

            //This line work perfect but it not redirect me to my application
            authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);

            //I want to send a calll back but It give exception
            ///***  authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URI.toString());

        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
    }

    @Override
    protected void onResume() {
        // this must be places in activity#onResume()  
        Uri uri = this.getIntent().getData();  
        if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {  
            String verifier = uri.getQueryParameter("oauth_verifier");  
            // this will populate token and token_secret in consumer  
            try {
                provider.retrieveAccessToken(consumer, verifier);
            } catch (OAuthMessageSignerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OAuthNotAuthorizedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OAuthExpectationFailedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OAuthCommunicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        }  
        super.onResume();
    }
}

而我的 AndroidManifest.xml 看起来像这样

<uses-permission
    android:name="android.permission.INTERNET"/>
<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
>
    <activity
        android:name=".Tweeter"
        android:label="@string/app_name"
    >
        <intent-filter>
            <action
                android:name="android.intent.action.MAIN"/>
            <category
                android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <intent-filter>
        <action
            android:name="android.intent.action.VIEW"
        ></action>
        <category
            android:name="android.intent.category.DEFAULT"
        ></category>
        <category
            android:name="android.intent.category.BROWSABLE"
        ></category>
        <data
            android:scheme="PicPuzzle"
            android:host="tkxel"
        ></data>
    </intent-filter>
</application>
<uses-sdk
    android:minSdkVersion="7"/>`enter code here`

【问题讨论】:

    标签: android callback twitter twitter-oauth


    【解决方案1】:

    回调 URL 应基于您在清单中为活动配置的 URL。看起来您正在使用scheme="PicPuzzle"host="tkxel"。所以你的回调地址是PicPuzzle://tkxel

    我认为 &lt;data&gt; 标记应该位于您想要接收回调的特定 Activity 上(看起来您目前在整个应用程序中都有它)。

    【讨论】:

    • 我尝试了这个但仍然没有解决方案......!它总是显示错误 oauth.signpost.exception.OAuthNotAuthorizedException:授权失败(服务器回复 401)。如果使用者密钥不正确或签名不匹配,则可能会发生这种情况。 01-10 16:53:20.614: WARN/System.err(5447): 在 oauth.signpost.AbstractOAuthProvider.handleUnexpectedResponse(AbstractOAuthProvider.java:239) 01-10 16:53:20.614: WARN/System.err(5447) :在 oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:189)
    • 我仔细查看了您的代码。我可以看到几个潜在的问题: - 消息签名者:设置为“consumer.setMessageSigner(new HmacSha1MessageSigner());” - 检索访问令牌。对验证者使用“OAuth.OAUTH_VERIFIER”(尽管这可能是同一件事。
    • 感谢您的考虑....但是将这段代码放在哪里?消息签名者:设置为“consumer.setMessageSigner(new HmacSha1MessageSigner());”
    • 在构建消费者之后立即。所以就在后面:“consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);”
    • 要确保的另一件事:在您的 twitter 应用程序设置中,确保“应用程序类型”设置为“浏览器”。您可以在此处使用任何回调 URL,因为它已被代码覆盖。
    猜你喜欢
    • 1970-01-01
    • 2012-06-16
    • 2015-02-12
    • 2011-01-13
    • 2011-11-15
    • 2023-04-09
    • 1970-01-01
    • 2015-04-13
    • 2014-03-14
    相关资源
    最近更新 更多