ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

我如何使用OAuth为Twitter回拨Android?

2019-05-15 20:16:32  阅读:162  来源: 互联网

标签:android twitter callback twitter-oauth


从过去的5天开始,我使用OAuth在android中搜索twitter的最佳工作代码…我发现了很多.但没有一个1完美运行.我怎么得到一些代码.它的工作,但我有一个问题.我喜欢用String callBack =“?”写的东西.这样我的android浏览器会将我的应用程序重定向到我的应用程序,而不是在认证后停留在那里.如果我没有使用CallBack并使用OAuth.OUT_OF_BAND,那么浏览器显示一个密码并且不会将浏览器重定向回我的应用程序.请帮我告诉我我做错了什么.

我的代码在这里

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`

最佳答案:

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

我认为< data> tag应该在你想要接收回调的特定Activity上(看起来你现在已经在整个应用程序上拥有它).

标签:android,twitter,callback,twitter-oauth
来源: https://codeday.me/bug/20190515/1109899.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有