Android 中 Twitter 回调问题 [英] Problem in Callback in Twitter in Android

查看:62
本文介绍了Android 中 Twitter 回调问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序中实现了 Twitter,我在回调中遇到了问题.

I have implemented Twitter in my application , i am facing problem in Callback.

Twitter API 最近已更新,因此我无法发送回调 URL.

Twitter API's has been updated recently so i am unable to send Callback URL.

现在设置页面也发生了变化,没有用于选择基于 Web 的应用程序或桌面应用程序的选项.

Also the Setting Page now change there is No Option for Selecting Web Based application or Desktop Application.

如果我在这条线上发送回调:

If i send Callback in this Line :

authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URL);

它总是返回

oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.

但如果我在此发送 Null重定向到 Twitter 登录页面,但在成功授权后,它不会返回到我的应用程序.

but if i send as Null in this its Redirecting to Twitter Login Page,but after successful Authorization it doesn't returns back to my Application.

显示密码后,我想重定向回我的应用程序.

After the Pin number is displayed i want to Redirect back to my Application.

注意:Twitter 更新了他们的 API,因此帖子中可用的旧代码无法使用.

Note : Twitter had updated their API so old Codes available in the Post is not working.

我尝试了以下所有链接

链接 1链接 2链接 3Link4 ,Link5 ,链接 6

我的代码如下:

public class Main extends Activity {
OAuthConsumer consumer;
OAuthProvider provider;
Twitter twitter;
private static  String CALLBACK_URL = "twitterapptest://connect";


@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    consumer = new DefaultOAuthConsumer(
            "XXXXXXXXXXX",
            "XXXXXXXXXXXXX");

    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 = null;
    try {
        authUrl = provider.retrieveRequestToken(consumer,null);
         this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
    } 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();
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String pin = null;
    try {
        pin = br.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        provider.retrieveAccessToken(consumer, "4947222");
    } 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();
    }

    URL url = null;
    try {
        url = new URL("http://twitter.com/statuses/mentions.xml");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    HttpURLConnection request = null;
    try {
        request = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        consumer.sign(request);
    } catch (OAuthMessageSignerException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (OAuthExpectationFailedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (OAuthCommunicationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        request.connect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        System.out.println("Response: " + request.getResponseCode() + " "
                + request.getResponseMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


/**
 * 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("XXXXXXX", "XXXXXXXXXX");
                twitter.setOAuthAccessToken(a);

                // create a tweet

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

                // send the tweet

                twitter.updateStatus(tweet);

            } catch (Exception e) {

                Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }
}

我的清单:

<?xml version="1.0" encoding="utf-8"?>

    <activity android:name=".OAuthForTwitter"  android:label="@string/app_name"
            android:configChanges="orientation|keyboardHidden"  android:launchMode="singleInstance">
     <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="twitterapptest" android:host="connect" />
    </intent-filter>

</activity>

</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET" />

推荐答案

问题在于回调 URl.我们应该在应用程序设置页面的回调 URL 的字段名称中提供一个虚拟回调 URL.

The problem is that Callback URl. We should give one Dummy Callback URL in the Field Name of CallBack URL in Application' s Settings Page.

如果我们这样做并在我们的代码中发送回调 URL,成功登录后将有一个名为重定向到您的应用程序

If we do like that and send the Call Back URL in our code, after successful Login there will be a option called Redirecting to your Application

如需进一步参考,请查看此 Twitter 链接

For Further Reference check this Link for Twitter

这篇关于Android 中 Twitter 回调问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆