【问题标题】:How to check callback_url in twitter application如何在 Twitter 应用程序中检查 callback_url
【发布时间】:2011-05-09 23:02:05
【问题描述】:

我有很多关于 O_Auth 的示例,但没有一个工作正常,我想唯一的问题是每个示例中的回调 URL,我收到 401 错误,例如使用者密钥或签名不匹配。我已经在 401 上看到了很多例子,唯一对我有利的是我的回调 url。

android app cannot connect to twitterGetting 401 when requesting access token with signpost within android with-signpost-within-android Android Dev - Callback URL not working... (0_o)

我已经看过所有这些示例以及更多示例。

但我仍然没有明白我的意思,如果在申请表时我在回调 url 使用http://www.meomyo.com,那么我应该在我的编码中使用它 像 android:scheme="?"机器人:主机=“?” 目前我正在使用其他示例回调

    //private static final Uri CALLBACK_URI = Uri.parse("bloa-app://twitt");
private static final Uri CALLBACK_URI = Uri.parse("twitterapp://connect");

我有消费者密钥和秘密密钥,但如果是回调 url,我会卡在它上面。 如果有人想要,我可以提供我的消费者以及密钥。

public class OAuth extends Activity {

private static final String APP =   "OAUTH";

private Twitter twitter;
private OAuthProvider provider;
private CommonsHttpOAuthConsumer consumer;

private String CONSUMER_KEY =       "Xh3o8Gh1JQnklkUnTvvA";
private String CONSUMER_SECRET =    "SCLy6yoUSth53goAsYYkoqR4ZuBoaInyJXsm5PQR11I";
private String CALLBACK_URL =       "merabharatmahan://piyush";

private TextView tweetTextView;
private Button buttonLogin;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tweetTextView = (TextView)findViewById(R.id.tweet);
    buttonLogin = (Button)findViewById(R.id.ButtonLogin);
    buttonLogin.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {
            askOAuth();
        }
    });
}

/**
 * Open the browser and asks the user to authorize the app.
 * Afterwards, we redirect the user back here!
 */
private void askOAuth() {
    try {
        consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
                                            "http://twitter.com/oauth/access_token",
                                            "http://twitter.com/oauth/authorize");
        String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
        Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
        this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
    } catch (Exception e) {
        Log.e(APP, e.getMessage());
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}


/**
 * As soon as the user successfully authorized the app, we are notified
 * here. Now we need to get the verifier from the callback URL, retrieve
 * token and token_secret and feed them to twitter4j (as well as
 * consumer key and secret).
 */
@Override
protected void onNewIntent(Intent intent) {

    super.onNewIntent(intent);

    Uri uri = intent.getData();
    if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {

        String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);

        try {
            // this will populate token and token_secret in consumer
            provider.retrieveAccessToken(consumer, verifier);

            // TODO: you might want to store token and token_secret in you app settings!!!!!!!!
            AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret());

            // initialize Twitter4J
            twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            twitter.setOAuthAccessToken(a);

            // create a tweet
            Date d = new Date(System.currentTimeMillis());
            String tweet = "#OAuth working! " + d.toLocaleString();

            // send the tweet
            twitter.updateStatus(tweet);

            // feedback for the user...
            tweetTextView.setText(tweet);
            Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();
            buttonLogin.setVisibility(Button.GONE);

        } catch (Exception e) {
            Log.e(APP, e.getMessage());
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }

    }
}

}

清单代码是

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".OAuth"
              android:label="@string/app_name"
              android:launchMode="singleInstance">
        <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="merabharatmahan" android:host="piyush" />
        </intent-filter>
    </activity>

</application

现在我与服务提供商的通信失败:收到的身份验证质询为空。这是我放在这里的最简单的例子。

【问题讨论】:

  • 按照上面的清单,首先改变 android:launchMode="singleTask", android:scheme 和 host 在我看来没问题。
  • 让我们知道您在 android 中使用的是哪个身份验证 jar?我正在使用 twitter4j、signpost-core 和 signpost-commonshttp jar 来实现 oAuth。
  • try { consumer.setTokenWithSecret(tempToken, tempSecretKey); provider.retrieveAccessToken(consumer, verifier); HttpParameters params = provider.getResponseParameters(); String userName = params.getFirst("screen_name"); String returnToken = consumer.getToken(); String returnSecret = consumer.getTokenSecret();

标签: android


【解决方案1】:

twitterapp://connect 是您的回调网址。

在 Android manifest.xml 中像这样定义你的活动:

<activity android:name=".auth.SocialNetworkActivity"
    android:configChanges="orientation"
        android:label="Connect SNS" android:launchMode="singleTask">
        <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="twitterapp" android:host="connect" />
        </intent-filter>
    </activity>

现在,它将打开 twitter 网页,并且在成功验证后,将调用您的活动 onNewIntent() 回调方法。

在上面的示例中,使用您自己的活动名称。

除此之外,我的应用程序中的 twitter 集成是使用 twitter4j 完成的。

【讨论】:

  • 嗨 Zoombie,我只是放了一个示例供参考,但我没有得到任何令牌,我可以在 DefaultOAuth.* 的帮助下在 java 上获取它,但 android 不支持它并且我的所有示例都说您需要签名密钥,并且在我的 java 程序中我使用的是 SignatureMethod.HMAC_SHA1 并且我正在获取令牌,但如果是 android... :(
  • +1。如果可以的话,我会 +27。值得一提的是,当你在 Twitter 上注册你的应用程序时,你必须说它是一个浏览器应用程序(并提供一个实际的互联网回调 URL),即使它是一个桌面应用程序,否则它将无法工作。
  • @Dan:是的,绝对正确,需要提供信息,无论 Twitter 上的 appln 是否是基于浏览器/客户端的应用程序。如果浏览器则需要回调 url。
【解决方案2】:

你必须放任何你认为独一无二的东西:

android:scheme="name-of-your-dog" android:host="something-else"

哦,等等……也许有人养了一只像你这样叫的狗……所以,使用你的应用程序的包名:

android:scheme="com.your.package" android:host="something-else"

而且,如果它不适合您...我认为它与回调 URL 无关。您如何在清单中声明您的活动?你加了android:launchMode="singleTask"参数对吧?

而且,正如僵尸家伙指出的那样,您必须在您的身份验证活动的 onNewIntent 方法中处理回调。像这样的:

final Uri uri = intent.getData();
if (uri != null && uri.getScheme().equals("name-of-your-dog")) {
    //etc...
}

【讨论】:

  • 仍然无法正常工作,我从最近两天开始一直在研究它,还有一件事要问我看到了各种样本,这些样本在他们的活动中使用回调 url,他们正在将这些东西定义到他们的清单中也喜欢类别和数据,但我正在使用回调到一个简单的 java 类而不是一个活动,所以这是清单无法识别它的原因,因为我没有在清单类别和数据中的任何地方定义它的 java 文件。
  • 是的,您必须首先在清单中定义它,并且代码应该处于活动状态,我在下面的回答只是解释了这一点
  • @PiyushMishra:必须执行 android manifest 的东西。看看@zoombie 在他的回答中是如何做到的……就这么简单。
  • 非常感谢大家让我努力工作,感谢您在我身上花费的宝贵时间。 :))
  • 您使用的是什么 OAuth 库?签个帖?你的错误到底是什么?推特页面启动后,您的活动不会被回调吗?你从哪里发布推特?
猜你喜欢
  • 2011-04-10
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 2013-05-11
  • 2021-08-29
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
相关资源
最近更新 更多