Android: Sign in with Twitter4J

What I tried:

I have already registered the application on Twitter and received the “Consumer Key” and “Secret”. Even I got different codes to enter twitter. This is what I tried:

http://thetechnib.blogspot.com/2011/01/android-sign-in-with-twitter.html

[This link is dead, you can view the archive here ]
http://www.android10.org/index.php/articleslibraries/291-twitter-integration-in-your-android-application

Problem:

Until now, the code above takes me to the twitter login and allows me to log in and have a PIN to complete the login process. But I have no idea how to use it to run my application. I checked all the code but found nothing related to the pin.

Secondly, when I registered my application on Twitter, he asked for a callback URL, but as it was written, it really is not needed, I skipped the indication. (Even I don’t know what it should be!)

And therefore, I give null as CallbackURL in my application.

Can anyone suggest to me how I can use this PIN to complete the login process and return the user to the main application of my application? Is this the callback url that is causing the problem, or is there something else I'm doing wrong?

Answer, please. Any help appriciated! Thanks.

EDIT:

As Frankenstein suggested, I tried the code at github.com/ddewaele/AndroidTwitterSample/downloads

I added my consumer key and consumer secret along with the callback url:

public static final String OAUTH_CALLBACK_SCHEME= "x-oauthflow-twitter"; public static final String OAUTH_CALLBACK_HOST= "callback"; public static final String OAUTH_CALLBACK_URL= OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST; 

but he gives me this error:

Logcat:

 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): Error during OAUth retrieve request token 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): 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. 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at oauth.signpost.AbstractOAuthProvider.handleUnexpectedResponse(AbstractOAuthProvider.java:239) 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:189) 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69) 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at com.ecs.android.sample.twitter.OAuthRequestTokenTask.doInBackground(OAuthRequestTokenTask.java:55) 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at com.ecs.android.sample.twitter.OAuthRequestTokenTask.doInBackground(OAuthRequestTokenTask.java:1) 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at android.os.AsyncTask$2.call(AsyncTask.java:185) 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at java.lang.Thread.run(Thread.java:1096) 

Also shows me a black screen when I press the TWEET button, instead of forcing me to sign up for a twitter screen.

Omg, I'll be crazy ... I’ve been trying since two days! :( please help.

+6
source share
3 answers

This is because your application is registered as a desktop client. To rewrite the callback URL, your application must be registered as a browser client.

Try setting up a dummy callback URL (http://example.com/or whatever) https://dev.twitter.com/apps/[appidapter/settings> Callback URL and your application will be recognized by the client browser.

Then try the @Frankenstein or @ jamn224 code.

+9
source

First you need to authenticate properly:

 try{ consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token", "http://twitter.com/oauth/access_token", "http://twitter.com/oauth/authorize"); String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL); startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); }catch(Exception e){ Log.e(TAG,e+""); } 

The need for CALLBACK_URL must be specified in the manifest file (see Frankenstein's answer). The above code begins the intention to authorize on the Twitter server. Callback information is necessary for the intent to know which application should return after the authorization process.

Then we need to process the return to the application after authentication on Twitter:

  @Override public void onResume(){ super.onResume(); if (this.getIntent()!=null && this.getIntent().getData()!=null){ Uri uri = this.getIntent().getData(); //handle returning from authenticating the user if (uri != null && uri.toString().startsWith(CALLBACK_URL)) { String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); String token = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_TOKEN); try { Twitter t = new TwitterFactory().getInstance(); t.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); // Get Access Token and store it rToken = new RequestToken(token, CONSUMER_SECRET); AccessToken aToken = t.getOAuthAccessToken(rToken); storeAccessToken(aToken); //send to checkLoginState again since we have authorization now! checkLoginState(); } catch (Exception e) { Log.e(TAG, e+""); } } } }//end onResume 

This code captures data from the return intent, including information to capture an authorization token. "storeAccessToken (aToken)" is a short method that I wrote that stores the token in the application settings, so we don’t need to repeat the authorization every time the application is opened.

Now that we have the authorization token, we can use it to authorize the Twitter instance:

 twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); twitter.setOAuthAccessToken(aToken); 

The aforementioned twitter variable is now authorized and can do its job.

+7
source

you must write a callback as shown in your activity

 <activity android:name="com.apps.twitter.PrepareRequestTokenActivity" 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> </activity> 

And in the constant file

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

Source: https://habr.com/ru/post/902614/


All Articles