Reputation: 441
I want to use Twitter oAuth
. I've looked different examples and tutorials. So I began from the outset. I've registered the application on dev.twitter.com,
it's okay. Consumer Key and Consumer Secret are available.
Then I've added signpost-core
and signpost-commonhttp
jars to my libs folder and defined constans like this:
private static final String CONSUMER_KEY = "myConsumerKey";
private static final String CONSUMER_SECRET = "myConsumerSecretKey";
private static String ACCESS_KEY = null;
private static String ACCESS_SECRET = null;
private static final String REQUEST_URL = "http://twitter.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "http://twitter.com/oauth/access_token";
private static final String AUTH_URL = "http://twitter.com/oauth/authorize";
private static final String CALLBACK_URL = "OauthTwitter://twitter";
private static final String PREFERENCE_FILE = "twitter_oauth.prefs"
private static CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(
CONSUMER_KEY, CONSUMER_SECRET);
private static CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(
REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL);
And here is my manifest:
<data android:scheme="OauthTwitter" android:host="twitter"/>
Finally here is my click event:
try {
String authURL = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Log.d("OAuthTwitter", authURL);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
When I clicked the button I saw this trace:
07-23 06:24:48.428: W/System.err(15121): 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.
07-23 06:24:48.433: W/System.err(15121): at oauth.signpost.AbstractOAuthProvider.handleUnexpectedResponse(AbstractOAuthProvider.java:243)
07-23 06:24:48.433: W/System.err(15121): at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:193)
07-23 06:24:48.433: W/System.err(15121): at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:74)
07-23 06:24:48.438: W/System.err(15121): at com.exampel.app.MyActivity$1.onClick(MyActivity.java:71)
07-23 06:24:48.438: W/System.err(15121): at android.view.View.performClick(View.java:2364)
07-23 06:24:48.438: W/System.err(15121): at android.view.View.onTouchEvent(View.java:4179)
07-23 06:24:48.438: W/System.err(15121): at android.widget.TextView.onTouchEvent(TextView.java:6541)
07-23 06:24:48.443: W/System.err(15121): at android.view.View.dispatchTouchEvent(View.java:3709)
07-23 06:24:48.443: W/System.err(15121): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-23 06:24:48.443: W/System.err(15121): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-23 06:24:48.448: W/System.err(15121): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-23 06:24:48.448: W/System.err(15121): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-23 06:24:48.448: W/System.err(15121): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1665)
07-23 06:24:48.448: W/System.err(15121): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-23 06:24:48.453: W/System.err(15121): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-23 06:24:48.453: W/System.err(15121): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1649)
07-23 06:24:48.453: W/System.err(15121): at android.view.ViewRoot.handleMessage(ViewRoot.java:1694)
07-23 06:24:48.458: W/System.err(15121): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 06:24:48.458: W/System.err(15121): at android.os.Looper.loop(Looper.java:123)
07-23 06:24:48.458: W/System.err(15121): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-23 06:24:48.463: W/System.err(15121): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 06:24:48.463: W/System.err(15121): at java.lang.reflect.Method.invoke(Method.java:521)
07-23 06:24:48.463: W/System.err(15121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-23 06:24:48.463: W/System.err(15121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-23 06:24:48.468: W/System.err(15121): at dalvik.system.NativeStart.main(Native Method)
I'm sure about key and secret but still the same. What am I doing wrong? Any suggestions would be great.
Upvotes: 2
Views: 1392
Reputation: 31
I had very similar code in place and was getting the same error. I was able to get things working by setting my test phone to use the network's date/time settings. Apparently if your device has incorrect date/time settings it can cause this issue.
Upvotes: 0
Reputation: 5317
First of all, I must specify your code and answers are both looking fine, but not sure why not working. Elsewhere, I'm using ConfigurationBuilder
while initializing TwitterFactory
.
It looks like:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("ConsumerKey")
.setOAuthConsumerSecret("ConsumerSecret");
try {
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
requestToken = twitter.getOAuthRequestToken("application://calback");
WebView twitterSite = new WebView(MyActivity.this);
twitterSite.loadUrl(requestToken.getAuthenticationURL());
setContentView(twitterSite);
} catch (Exception e) {
e.printStackTrace();
}
Then be careful about your <data>
definition. The tag should be into <intent-filter>
.
If still getting error, reset one more time. I hope this will solver your issue.
Upvotes: 4
Reputation: 71
I used this on click for Twitter Authentication..
Intent i = new Intent(getApplicationContext(), PrepareRequestTokenActivity.class);
finish();
startActivity(i);
and this is the PrepareRequestTokenActivity.java
package com.twitter;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import com.WFBOS.SettingActivity;
/**
* Prepares a OAuthConsumer and OAuthProvider
*
* OAuthConsumer is configured with the consumer key & consumer secret.
* OAuthProvider is configured with the 3 OAuth endpoints.
*
* Execute the OAuthRequestTokenTask to retrieve the request, and authorize the request.
*
* After the request is authorized, a callback is made here.
*
*/
public class PrepareRequestTokenActivity extends Activity
{
final String TAG = getClass().getName();
private OAuthConsumer consumer;
private OAuthProvider provider;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try
{
this.consumer = new CommonsHttpOAuthConsumer(Constants1.CONSUMER_KEY, Constants1.CONSUMER_SECRET);
this.provider = new CommonsHttpOAuthProvider(Constants1.REQUEST_URL,Constants1.ACCESS_URL,Constants1.AUTHORIZE_URL);
}
catch (Exception e)
{
Log.e(TAG, "Error creating consumer / provider",e);
}
Log.i(TAG, "Starting task to retrieve request token.");
new OAuthRequestTokenTask(this,consumer,provider).execute();
}
/**
* Called when the OAuthRequestTokenTask finishes (user has authorized the request token).
* The callback URL will be intercepted here.
*/
@Override
public void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
SharedPreferences prefs = getSharedPreferences("Twitter", MODE_PRIVATE);
final Uri uri = intent.getData();
if (uri != null && uri.getScheme().equals(Constants1.OAUTH_CALLBACK_SCHEME))
{
Log.i(TAG, "Callback received : " + uri);
Log.i(TAG, "Retrieving Access Token");
new RetrieveAccessTokenTask(this,consumer,provider,prefs).execute(uri);
finish();
}
}
public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void>
{
private Context context;
private OAuthProvider provider;
private OAuthConsumer consumer;
private SharedPreferences prefs;
public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,OAuthProvider provider, SharedPreferences prefs)
{
this.context = context;
this.consumer = consumer;
this.provider = provider;
this.prefs=prefs;
}
/**
* Retrieve the oauth_verifier, and store the oauth and oauth_token_secret
* for future API calls.
*/
@Override
protected Void doInBackground(Uri...params)
{
final Uri uri = params[0];
final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
try
{
provider.retrieveAccessToken(consumer, oauth_verifier);
final Editor edit1 = prefs.edit();
edit1.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
edit1.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
edit1.commit();
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
Log.d("toker", token);
Log.d("Secret", secret);
consumer.setTokenWithSecret(token, secret);
context.startActivity(new Intent(context,SettingActivity.class));
//executeAfterAccessTokenRetrieval();
Log.i(TAG, "OAuth - Access Token Retrieved");
}
catch (Exception e)
{
Log.e(TAG, "OAuth - Access Token Retrieval Error", e);
}
return null;
}
/*private void executeAfterAccessTokenRetrieval()
{
String msg = getIntent().getExtras().getString("tweet_msg");
try
{
TwitterUtils.sendTweet(prefs, msg);
}
catch (Exception e)
{
Log.e(TAG, "OAuth - Error sending to Twitter", e);
}
}*/
}
}
and here is the OAuthRequestTokenTask.java
package com.twitter;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
/**
* An asynchronous task that communicates with Twitter to
* retrieve a request token.
* (OAuthGetRequestToken)
*
* After receiving the request token from Twitter,
* pop a browser to the user to authorize the Request Token.
* (OAuthAuthorizeToken)
*
*/
public class OAuthRequestTokenTask extends AsyncTask<Void, Void, Void>
{
final String TAG = getClass().getName();
private Context context;
private OAuthProvider provider;
private OAuthConsumer consumer;
/**
*
* We pass the OAuth consumer and provider.
*
* @param context
* Required to be able to start the intent to launch the browser.
* @param provider
* The OAuthProvider object
* @param consumer
* The OAuthConsumer object
*/
public OAuthRequestTokenTask(Context context,OAuthConsumer consumer,OAuthProvider provider)
{
this.context = context;
this.consumer = consumer;
this.provider = provider;
}
/**
*
* Retrieve the OAuth Request Token and present a browser to the user to authorize the token.
*
*/
@Override
protected Void doInBackground(Void... params)
{
try
{
Log.i(TAG, "Retrieving request token from Google servers");
final String url = provider.retrieveRequestToken(consumer, Constants1.OAUTH_CALLBACK_URL);
Log.i(TAG, "Popping a browser with the authorize URL : " + url);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
context.startActivity(intent);
}
catch (Exception e)
{
Log.e(TAG, "Error during OAUth retrieve request token", e);
}
return null;
}
}
This is my Constants1.java
package com.twitter;
public class Constants1
{
public static final String CONSUMER_KEY = "your key";
public static final String CONSUMER_SECRET= "your key";
public static final String REQUEST_URL = "http://api.twitter.com/oauth/request_token";
public static final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
public static final String AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize";
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;
//public static final String OAUTH_CALLBACK_URL = "http://www.wfbos.com";
}
Upvotes: 1
Reputation: 10856
reset key from twitter developer account and try again.. i have same problem and after reseting key problem solved.
use this url
public static final String REQUEST_URL = "http://api.twitter.com/oauth/request_token";
public static final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
public static final String AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize";
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;
Upvotes: 4