Problem in Twitter callback on Android

I implemented Twitter in my application, I ran into a problem in the callback.

The Twitter API has recently been updated, so I cannot send the callback URL.

Also now on the settings page now there is no option to select a web application or a working application.

If I send a callback on this line:

authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URL); 

He always returns

 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. 

but if I send Null to this Redirect to the Twitter login page , but after successful authorization it will not return back to my application.

After displaying the contact number, I want to redirect back to my application.

Note. Twitter has updated its API, so the old codes available in Post do not work.

I tried all of the following links

Link 1 , Link 2 , Link 3 , Link4 , Link5 , Link 6

My code is as follows:

 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(); } } } } 

My manifest:

 <?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" /> 

+4
source share
3 answers

The problem is that Callback URl. We need to specify one callback URL in the Name field of the CallBack URL field on the application settings page.

If we do this and send the callback URL to our code, after successful login, the Redirect to your application option will appear

For more help check out the Twitter link.

+8
source

Did you find where the problem is? I had one and the same performance, and finally I found where it came from. It’s true that the Twitter settings page has changed and you can no longer choose a web application or desktop application. But here are some tips: in the settings of your Twitter application, simply fill in the callback URL with a mannequin, for example http://www.dummy.com . This will imply that your application has a web browser, and then when you send your own callback, it will replace the dummy. I spent many times to find this, so hope this answer helps someone.

+3
source

Adding the Callback URL below to the application will solve the problem. It will redirect the user to Application , which will launch him to authenticate the user 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 necessary 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 you need. e.g. getHost , getScheme , getEncodedQuery , getQuery , getEncodedSchemeSpecificPart and more according to your needs, using intent , which are returned by the callback

 @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 helps everyone _

0
source

All Articles