400 Bad Request in Yahoo Authentication
Asked Answered
C

2

8

I'm trying to integrate Yahoo into my application.

I want users to login using their Yahoo accounts but whenever I request for a token, I receive the following errors:

getRequestToken() Exception: oauth.signpost.exception.OAuthCommunicationException: 
Communication with the service provider failed: Service provider responded in error: 400 (Bad Request)

Here is my code (Request_Token_Activity.java):

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.signature.HmacSha1MessageSigner;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

public class Request_Token_Activity extends Activity {
    private OAuthConsumer consumer; 
    private OAuthProvider provider;
    private SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {

        consumer = new CommonsHttpOAuthConsumer("my consumer key", "my consumer secret");
        consumer.setMessageSigner(new HmacSha1MessageSigner()); 
        provider = new CommonsHttpOAuthProvider(
                "http://api.login.yahoo.com/oauth/v2/get_request_token",
                "http://api.login.yahoo.com/oauth/v2/get_token",
                "http://api.login.yahoo.com/oauth/v2/request_auth");

    } catch (Exception e) {
        Log.e("", "onCreate Exception: " + e.toString());
    }
    getRequestToken();
}
private void getRequestToken() {
    try {

        String url = provider.retrieveRequestToken(consumer, "yahooapi://callback");
        Log.i("", "Yahoo 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);
        this.startActivity(intent);
    } catch (Exception e) {
        Log.i("", "getRequestToken() Exception: " + e.toString());
    }
}
@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent); 
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    final Uri uri = intent.getData();
    if (uri != null && uri.getScheme().equals("yahooapi")) {
        getAccessToken(uri);
    }
}
private void getAccessToken(Uri uri) {
    final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
    try {
        provider.retrieveAccessToken(consumer, oauth_verifier);

        final Editor edit = prefs.edit();
        edit.putString("YAHOO_OAUTH_TOKEN", consumer.getToken());
        edit.putString("YAHOO_OAUTH_TOKEN_SECRET", consumer.getTokenSecret());
        edit.commit();

        String token = prefs.getString("YAHOO_OAUTH_TOKEN", "");
        String secret = prefs.getString("YAHOO_OAUTH_TOKEN_SECRET", "");
        consumer.setTokenWithSecret(token, secret);

        Log.i("", "Yahoo OAuth Token: " + token);
        Log.i("", "Yahoo OAuth Token Secret: " + token);

    } catch (Exception e) {
        Log.i("", "getAccessToken Exception: " + e.toString());
    }
}

}

And this is a snapshot of my AndroidManifest.xml:

        <activity android:name="Request_Token_Activity" android:launchMode="singleTask">
        <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="yahooapi" android:host="callback" />
        </intent-filter>
    </activity>

I have set-up my Yahoo Project as a Web Application and put Read and Write access to Social and Contacts. What am I doing wrong?

Cullum answered 6/7, 2012 at 3:12 Comment(1)
I don't know if this applies here or not, but I remember that Yahoo! sometimes requires you to be on wifi to connect to their servers. Again, I'm not sure if this applies here or is even the case anymore with Yahoo!Glaudia
V
4

Looks like yahoo's api call is an ssl call. I don't know if it's that simple but your code has the Oauth providers as:

           "http://api.login.yahoo.com/oauth/v2/get_request_token",
            "http://api.login.yahoo.com/oauth/v2/get_token",
            "http://api.login.yahoo.com/oauth/v2/request_auth");

according to http://developer.yahoo.com/oauth/guide/oauth-requesttoken.html

The provider should be Http*s*//api.login.yahoo.com/oauth/v2/get_request_token

additionally here are the yahoo oauth error codes and the reasons you would receive a 400 error http://developer.yahoo.com/oauth/guide/oauth-errors.html I would log my my request and see what paramater is missing/wrong/malformed etc.

Variolous answered 12/7, 2012 at 15:49 Comment(0)
L
3

@Jayson Tamayo i agree with Eliezer, try using your app on wifi. I tried in wifi and it works,but sadly its not working on GPRS.If your android application is a native app , then you should set your project as Client/Desktop. I have done it and it works for me.

Labradorite answered 11/7, 2012 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.