views:

85

answers:

3

Hi In my android app I can make calls to Twitter api immediately after I login. I am using same instance of OAuthConsumer. But when I create OAuthconsumer for subsequent call and use setTokenWithSecret I get Incorrect signature error.

I spent few hours trying to debug but no luck... any help is appreciated.

Following is code ...in onnewIntent if statement 1== 1 works but I get Incorrect signature error If I call verify method on Twitter adapter

private static String TAG = "OAuthForTwitter";

private CommonsHttpOAuthConsumer httpOauthConsumer;
private OAuthProvider httpOauthprovider;
public final static String consumerKey = "";
public final static String consumerSecret = "";
private final String CALLBACKURL = "myapp://mainactivity";


@Override
protected void onNewIntent(Intent intent) {

    super.onNewIntent(intent);
    Log.d(TAG, "onNewIntent");

    Uri uri = intent.getData();

    if (uri != null && uri.toString().startsWith(CALLBACKURL)) {

        String verifier = uri
                .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);

        Log.d(TAG, "onNewIntent  " + " verifier " + verifier);
        try {

            httpOauthprovider.retrieveAccessToken(httpOauthConsumer,
                    verifier);
            String userKey = httpOauthConsumer.getToken();
            String userSecret = httpOauthConsumer.getConsumerSecret();

            if (1 == 1) {
                String surl = "http://api.twitter.com/1/account/verify_credentials.xml";

                HttpGet request = null;
                HttpClient httpClient = null;
                HttpResponse response = null;
                request = new HttpGet(surl);
                httpOauthConsumer.sign(request);
                System.out.println("Sending request to Twitter...");
                httpClient = new DefaultHttpClient();

                response = httpClient.execute(request);
                String sresponse = parseResponseToString(response);
                Log.d(TAG, sresponse);
            } else {

                TwitterAdapter adapter = new TwitterAdapter(null,
                        consumerKey, consumerSecret, userKey, userSecret);
                String s = adapter.VerifyUser();
                Log.d(TAG, s);
            }

        } catch (Exception e) {
            Log.d(TAG, "onNewIntent error " + e.getMessage());
        }

    }
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    doOauth();
}

private void doOauth() {
    try {
        httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,
                consumerSecret);
        httpOauthprovider = new DefaultOAuthProvider(
                "http://twitter.com/oauth/request_token",
                "http://twitter.com/oauth/access_token",
                "http://twitter.com/oauth/authorize");

        String authUrl = httpOauthprovider.retrieveRequestToken(
                httpOauthConsumer, CALLBACKURL);

        this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                .parse(authUrl)));
        Log.d(TAG, "sent doOauth");
    } catch (Exception e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
    Log.d(TAG, " doOauth Complete");
}


public class TwitterAdapter {

    oauth.signpost.commonshttp.CommonsHttpOAuthConsumer httpOauthConsumer;

    public TwitterAdapter(String username, String consumerkey,String consumersecret, String accesstoken, String accesssecret) {

        httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerkey, consumersecret);

        httpOauthConsumer.setTokenWithSecret(accesstoken, consumersecret);
    }


    public String VerifyUser() throws ClientProtocolException, IOException,
            OAuthMessageSignerException, OAuthExpectationFailedException,
            OAuthCommunicationException {

        String surl = "http://api.twitter.com/1/account/verify_credentials.xml";

        HttpGet request = null;
        HttpClient httpClient = null;
        HttpResponse response = null;
        request = new HttpGet(surl);
        httpOauthConsumer.sign(request);
        System.out.println("Sending request to Twitter...");
        httpClient = new DefaultHttpClient();

        response = httpClient.execute(request);
        return parseResponseToString(response);

    }
}

}

A: 

Have you tried using Scribe ?

It has a working example using Twitter and it's android-ready.

Pablo Fernandez
I think I will give it a try. I have not been able to figure out why above code is not working.
Yogesh
I might be able to help you if you do, since I'm the creator of scribe. :)
Pablo Fernandez
In your example u have following code OAuthService service = new ServiceBuilder() .provider(TwitterApi.class) .apiKey("6icbcAXyZx67r8uTAUM5Qw") .apiSecret("SCCAdUUc6LXxiazxH3N0QfpNUvlUy84mZ2XZKiv39s") .build();
Yogesh
does that code need to be executed every time application starts
Yogesh
I tried your code it works f I use as it is but if I change it to suit my application it does not work. I must have missed something about OAuth. Bu here is what I am doing
Yogesh
I think when I recreate Token with Token aToken = new Token(accesstoken, accesssecret);
Yogesh
it does not work
Yogesh
A: 

I tried your code... it works if I use as it is but if I change it to suit my application it does not work. I don't get back response for verify_credentials call. I must have missed something about OAuth. But here is what I am doing

public class scribeauth extends Activity {
    private static String TAG = "OAuthForTwitter";
    public final static String consumerKey = "";
    public final static String consumerSecret = "";
    private final String CALLBACKURL = "myapp://mainactivity";
    private static final String AUTHORIZE_URL = "https://twitter.com/oauth/authorize?oauth_token=";
    private static final String PROTECTED_RESOURCE_URL = "http://api.twitter.com/1/account/verify_credentials.xml";
    OAuthService service = null;
    Token requestToken = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        doOauth();
    }

    private void doOauth() {
        try {

            service = new ServiceBuilder().provider(TwitterApi.class)
                    .apiKey(consumerKey)
                    .apiSecret(consumerSecret)
                    .callback(CALLBACKURL).build();

            requestToken = service.getRequestToken();

            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                    .parse(AUTHORIZE_URL + requestToken.getToken())));

        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
        Log.d(TAG, " doOauth Complete");
    }

    @Override
    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);
        Log.d(TAG, "onNewIntent");

        Uri uri = intent.getData();

        if (uri != null && uri.toString().startsWith(CALLBACKURL)) {

            String sverifier = uri
                    .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);

            Verifier verifier = new Verifier(sverifier);

            Token accessToken = service.getAccessToken(requestToken, verifier);

            String secret = accessToken.getSecret();
            String token = accessToken.getToken();

            new TwitterAdapter().VerifyUser(consumerKey, consumerSecret, secret, token);

        }
    }   

    public class TwitterAdapter {
        public String VerifyUser(String consumerkey, String consumersecret,
                String accesstoken, String accesssecret) {

            service = new ServiceBuilder().provider(TwitterApi.class)
            .apiKey(consumerkey)
            .apiSecret(consumersecret)
            .callback(CALLBACKURL).build();
            OAuthRequest request = new OAuthRequest(Verb.GET,PROTECTED_RESOURCE_URL);

            Token accessToken = new Token(accesstoken, accesssecret);
            service.signRequest(accessToken, request);
            Response response = request.send();
            String sresponse = response.getBody();
            Log.d(TAG, sresponse);
            return sresponse;
        }
    }
}
Yogesh
What's the error you're getting?
Pablo Fernandez
Oh and you don't need to create the service every time. Just do it once in the constructor (it's threadsafe so don't worry)
Pablo Fernandez
I get 401 error and response.getBody() returns null
Yogesh
Please can you provide a gist with a main method failing?
Pablo Fernandez