【问题标题】:twitter is not redirecting to the callback url in android apptwitter 没有重定向到 android 应用程序中的回调 url
【发布时间】:2012-08-25 03:58:44
【问题描述】:

我的目标是允许使用 twitter4j 登录 Twitter。 I used this as reference.

问题是在第 64 行,它调用了getAuthenticationURl()。当我执行时,它会将我带到登录页面,而不是我可以允许/禁止我的应用访问 ppl 帐户的页面。
但是,如果我将其更改为getAuthorizationUrl(),它将转到正确的页面。

1) 那么两者有什么区别呢?

2) 这是一个重要的问题。我提供下面的代码。

package com.example.twitter;



import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class WriteTweet extends Activity{
    public final static String consumerKey = "xxxxxxxxxxxxxxxxxx";
    public final static String consumerSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    private final String CALLBACKURL = "T4J_OAuth://callback";
    Twitter twitter;
    RequestToken requestToken;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        loginUser();
    }

    private void loginUser() {
        // TODO Auto-generated method stub
        try {
            twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(consumerKey, consumerSecret);
            requestToken = twitter.getOAuthRequestToken(CALLBACKURL);
            String authUrl = requestToken.getAuthorizationURL();
            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                    .parse(authUrl)));
        } catch (TwitterException ex) {
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
            Log.e("in Main.OAuthLogin", ex.getMessage());
        }
    }
    @Override
    protected void onNewIntent(Intent intent) {
        Log.e("hi","hi");
        super.onNewIntent(intent);
        Uri uri = intent.getData();
        try {
            String verifier = uri.getQueryParameter("oauth_verifier");
            AccessToken accessToken = twitter.getOAuthAccessToken(requestToken,
                    verifier);
            String token = accessToken.getToken(), secret = accessToken
                    .getTokenSecret();
            displayTimeLine(token, secret); //after everything, display the first tweet 

        } catch (TwitterException ex) {
            Log.e("Main.onNewIntent", "" + ex.getMessage());
        }

    }
    @SuppressWarnings("deprecation")
    void displayTimeLine(String token, String secret) {
        Log.e("display","timeline");
    }

}

清单是

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.twitter"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application

        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>


        <activity android:name=".ListTweets" >

        </activity>
        <activity android:name=".WriteTweet"> 
                <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="T4J_OAuth" android:host="callback" />
                </intent-filter>
        </activity>
        <activity android:name=".ViewFollowers">

        </activity>
    </application>

</manifest>

问题是它会将我重定向到https://mobile.twitter.com/ 而且我无法弄清楚做错了什么。不管是getAuthenticationUrl()还是getAuthorizationUrl()都存在这个问题。我在 Twitter 的 callback_url 字段中提供了一个虚拟 url。 如果我没有在代码中提供callbackurl,并且在推特页面中它会给我一个密码。但这不是我想要的。我需要我的推特重定向到我的应用程序。我怎样才能做到这一点?

【问题讨论】:

    标签: android twitter callback twitter4j


    【解决方案1】:

    这就是我解决问题的方法,

    1. 创建一个 web 视图并从您的应用程序中加载 twitter 的配置页面。

      vw = (WebView)findViewById(R.id.loginView);
      vw.setWebViewClient(new WebViewClient() {
          @Override
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
              return super.shouldOverrideUrlLoading(view, url);
          }
      }); vw.loadUrl(authUrl);
      
    2. 收到 callback_url 后,获取“oauth_verifier”

                  Uri uri = Uri.parse(url);
                  String verifier = uri.getQueryParameter("oauth_verifier");
      
    3. 使用此验证器获取访问权限并执行您想要的操作。在我的情况下,我销毁了 webview 并加载了我需要的活动。

    【讨论】:

      【解决方案2】:

      使用下面的 URL 作为回调 URL。

      public static final String  CALLBACK_URL = "x-oauthflow-twitter://callback";
      

      有关 android 应用程序中的 twitter 集成的更多信息,请参阅下面的链接。

      Twitter Integration

      【讨论】:

      • 不,它没有按预期工作。现在处于回调循环中。它一直重定向到不同的授权页面。
      • @emiljho 有关更多信息,请参见上面的链接,它在我的应用程序中工作。
      • 这里是根据你的博客gist.github.com/3528561的代码。它不工作。我认为问题不在于 callback_url。
      • @emiljho 您是否在哪个 API 版本上测试此应用程序?
      猜你喜欢
      • 2013-07-03
      • 2021-02-10
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 2012-05-26
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多