Problem in Callback in Twitter in Android

帅比萌擦擦* 提交于 2019-11-28 11:38:01

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.

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

For Further Reference check this Link for Twitter

Have you found where the problem come from? I had the same one exeception and finally I found where this come from. That right that the setting page of Twitter had change and you can't no more selecting Web Based application or Desktop Application. But here is the tips: in settings of your Twitter application just fill the Callback URL with a dummy one like http://www.dummy.com . This will implicitly set your application has a web browser and then when you send your own callback it will remplace the dummy one. I had spend a lot of times to find this so I hope this answer will help someone.

Adding the below Callback URL in to app would solve the problem. It will redirect user to the Application which start it to authenticate user's Twitter account_

update in Manifest_

<activity android:name="<YOUR ACTIVITY NAME>"
        android:launchMode="singleTask" android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:screenOrientation="portrait">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="x-oauthflow-twitter" android:host="callback" />
        </intent-filter>

In Your TwitterManager where you have all your TwitterFactory and the required things_

final public static String  CALLBACK_SCHEME = "x-oauthflow-twitter";    
final public static String  CALLBACK_URL = CALLBACK_SCHEME + "://callback";
public static final String TWITTER_IEXTRA_OAUTH_VERIFIER = "oauth_verifier";

Finally you can get everything that you need_ e.g., getHost, getScheme, getEncodedQuery, getQuery, getEncodedSchemeSpecificPart and more as per your need using the intent that return by the call back_

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

    new AsyncTask<Void,Void,Void>(){
        @Override
        protected Void doInBackground(Void... arg0) {
            Uri uri = intent.getData();
            if (uri != null && uri.toString().startsWith(TwitterManager.TWITTER_CALLBACK_URL)) {
                String verifier = uri.getQueryParameter(TwitterManager.TWITTER_IEXTRA_OAUTH_VERIFIER);
                Log.e("---ActivityMain-onNewIntent---", "verifier:"+verifier+", uri- getQuery:"+uri.getQuery());
                Log.i(ApplicationPockets.TAG, "verifier : "+verifier);
                if(verifier != null){
                    try {
                     /*
                      *---Get the AccessToken and do what you like ... :)
                      */
                        AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
                       SharedPreferences.Editor e = context.getSharedPreferences(SF_TWITTER, Context.MODE_PRIVATE).edit();
                       e.putString(TWITTER_PREF_KEY_TOKEN, accessToken.getToken());
                       e.putString(TWITTER_PREF_KEY_SECRET, accessToken.getTokenSecret());
                      e.commit();
                     //Extra you would like to do...
                    } catch (TwitterException e) {
                        e.printStackTrace();
                    }
                }else{
                    //Logout Twitter.
                }
            }
            return null;
        }
    }.execute();
}

Your RequestToken_

  try {
         RequestToken requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
        //Toast.makeText(activity, "Please authorize this app!", Toast.LENGTH_LONG).show();
        activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL())));
      } catch (TwitterException e) {

          e.printStackTrace();
    }

I hope this will help everyone_

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!