android - 授权成功后如何获取AccessToken发布推特

标签 android twitter twitter-oauth

这一直很痛苦。我不知道我对 AccessToken 做错了什么,但我尝试在调用 Twitter4j 的 updateStatus 发布到 Twitter 之前设置它。

我尝试了不同的方法来获取 AccessToken,我只使用 Twitter4J。

AccessToken accessToken = twitter.getOAuthAccessToken(twitter.getOAuthRequestToken(), verifier);

给出错误:

05-09 12:06:45.776: D/GameName(678): Received authentication challenge is null

我可以切换到 Twitter 浏览器进行授权。我输入我的用户名和密码,按“授权应用程序”。然后浏览器更改为“重定向”回我的应用程序。我的应用程序出现并繁荣!它崩溃了!我的应用调用了 onNewIntent。

授权代码效果很好: (twitter是成员变量)

    public void twitterAuthorize() 
    {   
        try 
        {
            twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);

            RequestToken requestToken = twitter.getOAuthRequestToken(Constants.OAUTH_CALLBACK_URL);                         
            startActivityForResult(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthorizationURL())), TWITTER_AUTHORIZE_ACTIVITY_RESULT_CODE); 
        } 
        catch (TwitterException e) 
        { 
                Log.d(Globals.sApplicationName, e.getMessage());
                e.printStackTrace();                 
        }       

    }

从授权返回,我到达“AccessToken accessToken =...”行,抛出异常。

@Override
protected void onNewIntent(Intent intent) 
{
    super.onNewIntent(intent);

    Log.d(Globals.sApplicationName, "ENTERED: onNewIntent");

    Uri uri = intent.getData();
    if (uri != null && uri.toString().startsWith(Constants.OAUTH_CALLBACK_URL)) 
    {
        Log.d(Globals.sApplicationName, "oAuth: " + uri.toString());
        String verifier = uri.getQueryParameter("oauth_verifier");

        try {
            if (verifier != null)
            {
                Log.d(Globals.sApplicationName, "Verifier: " + verifier);


                //Try 1: doesn't work
                //AccessToken a = new AccessToken(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);

                //Try 2: doesn't work               
                AccessToken accessToken = twitter.getOAuthAccessToken(twitter.getOAuthRequestToken(), verifier);

                //Try 3: doesn't work
                //String token = uri.getQueryParameter("oauth_token");      
                //AccessToken accessToken = new AccessToken(token, verifier);

                //twitter = new TwitterFactory().getInstance();

                twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
                twitter.setOAuthAccessToken(accessToken);

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

                // send the tweet
                twitter.updateStatus(tweet);
            }
        } catch (Exception e) {

            Log.d(Globals.sApplicationName, e.getMessage());
            e.printStackTrace();
        }
    }
}

我在调用堆栈上遇到了这些错误(我刚刚替换了,所以我在这篇论坛帖子中将其保密):

05-09 12:06:45.136: D/GameName(678): ENTERED: onNewIntent
05-09 12:06:45.136: D/GameName(678): oAuth: GameNameTwitterOAuth://callback?oauth_token=<some long string>&oauth_verifier=<some long string>
05-09 12:06:45.156: D/GameName(678): Verifier: <some long string>
05-09 12:06:45.747: W/AudioFlinger(32): write blocked for 131 msecs, 825 delayed writes, thread 0xff88
05-09 12:06:45.776: D/GameName(678): Received authentication challenge is null
05-09 12:06:45.846: W/System.err(678): Received authentication challenge is nullRelevant discussions can be on the Internet at:
05-09 12:06:45.846: W/System.err(678):  http://www.google.co.jp/search?q=bfb606ed or
05-09 12:06:45.856: W/System.err(678):  http://www.google.co.jp/search?q=72d7e395
05-09 12:06:45.856: W/System.err(678): TwitterException{exceptionCode=[bfb606ed-72d7e395 e2110e48-e8248ad2], statusCode=-1, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.5}
05-09 12:06:45.856: W/System.err(678):  at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:200)
05-09 12:06:45.856: W/System.err(678):  at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
05-09 12:06:45.856: W/System.err(678):  at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
05-09 12:06:45.856: W/System.err(678):  at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:121)
05-09 12:06:45.856: W/System.err(678):  at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
05-09 12:06:45.856: W/System.err(678):  at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:276)
05-09 12:06:45.856: W/System.err(678):  at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:269)
05-09 12:06:45.856: W/System.err(678):  at com.mnecreations.panarchyfling.PanarchyFling.onNewIntent(PanarchyFling.java:602)
05-09 12:06:45.869: W/System.err(678):  at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1122)
05-09 12:06:45.869: W/System.err(678):  at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:1812)
05-09 12:06:45.869: W/System.err(678):  at android.app.ActivityThread.performNewIntents(ActivityThread.java:1825)
05-09 12:06:45.869: W/System.err(678):  at android.app.ActivityThread.handleNewIntent(ActivityThread.java:1834)
05-09 12:06:45.876: W/System.err(678):  at android.app.ActivityThread.access$2300(ActivityThread.java:123)
05-09 12:06:45.876: W/System.err(678):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1040)
05-09 12:06:45.876: W/System.err(678):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 12:06:45.876: W/System.err(678):  at android.os.Looper.loop(Looper.java:126)
05-09 12:06:45.876: W/System.err(678):  at android.app.ActivityThread.main(ActivityThread.java:3997)
05-09 12:06:45.876: W/System.err(678):  at java.lang.reflect.Method.invokeNative(Native Method)
05-09 12:06:45.876: W/System.err(678):  at java.lang.reflect.Method.invoke(Method.java:491)
05-09 12:06:45.896: W/System.err(678):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-09 12:06:45.896: W/System.err(678):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-09 12:06:45.896: W/System.err(678):  at dalvik.system.NativeStart.main(Native Method)
05-09 12:06:45.896: W/System.err(678): Caused by: java.io.IOException: Received authentication challenge is null
05-09 12:06:45.896: W/System.err(678):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:1176)
05-09 12:06:45.896: W/System.err(678):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:1118)
05-09 12:06:45.896: W/System.err(678):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1044)
05-09 12:06:45.896: W/System.err(678):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:736)
05-09 12:06:45.906: W/System.err(678):  at twitter4j.internal.http.HttpResponseImpl.<init>(HttpResponseImpl.java:35)
05-09 12:06:45.906: W/System.err(678):  at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:164)
05-09 12:06:45.906: W/System.err(678):  ... 21 more
05-09 12:06:45.906: D/GameName(678): --------------------------------------------
05-09 12:06:45.906: D/GameName(678): Restart activity GameName

请指教,这真是令人费解。我查看了其他帖子,他们提到了有关时间戳的内容,但这对我来说没有意义。

谢谢

最佳答案

我认为您正在生成一个与您拥有的验证程序无关的新请求 token 。

尝试保存 twitter 对象和您第一次请求的 token 并尝试执行如下操作:

  1. 列表项
  2. 使用 consumerkey 和 consumerkeysecret 从 twitter factory 获取 twitter 对象
  3. 获取请求 token (twitter.getrequesttoken())
  4. 向 requesttoken 请求授权 url
  5. 保存 twitter 对象和请求 token
  6. 转到 Twitter 并验证
  7. 通过回调返回并检索验证者
  8. 获取保存的 twitterobject 和 requesttoken
  9. 那些保存的 twitterobject 和 requesttoken 应该与验证器一起使用来验证用户....

那么你就通过了身份验证

关于android - 授权成功后如何获取AccessToken发布推特,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10509834/

相关文章:

php - 具有多个用户位置的 Google map Android 应用

Webview 中的 Android 2.3 希伯来语支持

android - 将 MediaController 保持在 VideoView 的屏幕上

perl - 无法使用twitter api上传图片

java - 使用 twitter4j 发布推文

java - 在带有回调 url 的 Twitter 中使用 Scribe 库进行 Oauth

android:使用 FFMPEG 将两个音频文件合并为一个

php - 如何缓存 twitter api 结果?

android - Android 的 Twitter 客户端是否使用 OAuth?

java - 浏览器不会将我重定向到 Twitter 进行授权