【问题标题】:What to put at CALLBACK_URL in twitter using Oauth from my android application?使用我的 android 应用程序中的 Oauth 在 Twitter 中的 CALLBACK_URL 中放置什么?
【发布时间】:2011-04-10 13:58:33
【问题描述】:

我正在开发 Twitter -Client 的应用程序。我从这个网站得到了很多提示。我写了一些来是

import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

public class TwitterConnetcion extends Activity {

     private static final String APP ="OAUTH";

     private Twitter twitter;

     private OAuthProvider provider;

     private CommonsHttpOAuthConsumer consumer;


     private String CONSUMER_KEY ="MyConsumerKey";

     private String CONSUMER_SECRET ="MyConsumersecrete";
     private String CALLBACK_URL ="SoftDroidbyDhrumil://twitterconnetcion";///SoftDroid is my twitter //Apllication that i registered in Twitter site
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            provider = new DefaultOAuthProvider("https://api.twitter.com/oauth/request_token","https://api.twitter.com/oauth/access_token","https://api.twitter.com/oauth/authorize");

            String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
            Log.i("Dhrumil",authUrl);
            Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();

            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
            Log.i("Dhrumil","Browser Start"); } catch (Exception e) {
            Log.i("Dhrumil"+APP, e.toString());
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
    protected void onNewIntent(Intent intent) {
        // TODO Auto-generated method stub
        super.onNewIntent(intent);

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

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

                Log.i("Dhrumil verifier",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());
                        Log.i("Dhrumil AccessToken",a.toString());
                        // 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...

                        Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();


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



    }
    }}

我编译正常并运行此应用程序,它将在 Twitter 网站上重定向我以获得“允许”许可。然后我想回到我的 android 应用程序,但出现错误”找不到“SoftDroidbyDhrumil://twitterconnetcion?OuathToken=somoething”

我在我的回调 URL 上写什么,以便我可以从 android 浏览器返回到我的应用程序?

【问题讨论】:

    标签: android url twitter callback oauth


    【解决方案1】:

    您需要针对特定​​ URL 的 Intent 过滤器,您的应用将在访问时处理该 URL。

    所以你会重定向到类似的东西:

    yourcustomscheme://witterconnetcion?OuathToken=somoething

    在下一页的中间,它被描述了 http://developer.android.com/guide/topics/intents/intents-filters.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-10
      • 2011-06-08
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 2012-07-02
      相关资源
      最近更新 更多