android - Yahoo 身份验证中的 400 错误请求

标签 android yahoo yahoo-api

我正在尝试将 Yahoo 集成到我的应用程序中。

我希望用户使用他们的 Yahoo 帐户登录,但每当我请求 token 时,我都会收到以下错误:

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

这是我的代码(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());
    }
}

}

这是我的 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>

我已将我的 Yahoo Project 设置为 Web 应用程序 并将读取和写入访问权限 设置为 Social联系人。我做错了什么?

最佳答案

看起来yahoo 的api 调用是一个ssl 调用。我不知道是不是这么简单,但您的代码将 Oauth 提供程序设置为:

           "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");

根据 http://developer.yahoo.com/oauth/guide/oauth-requesttoken.html

提供者应该是Http*s*//api.login.yahoo.com/oauth/v2/get_request_token

此外,这里还有 yahoo oauth 错误代码以及您收到 400 错误的原因 http://developer.yahoo.com/oauth/guide/oauth-errors.html我会记录我的请求并查看缺少/错误/格式错误等参数。

关于android - Yahoo 身份验证中的 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11355340/

相关文章:

ssl - 带有 Yahoo! 的 Heroku 应用程序托管域 SSL

javascript - 如何从 html 文档中调用 yahoo 天气 api javascript 函数

android - 在 Play Store 上发布 Android app bundle 有没有上限?

javascript - 雅虎广告的麻烦警报框

android - 当我必须使用 Intent 时,什么是 createChooser?我可以用这种方法做什么?

css - 我的 Tumblr 博客上的此页面不安全(损坏的 HTTPS)

javascript - 尝试使用 jquery/php/xml 检索雅虎天气

yahoo-api - 将注音假名添加到网站内容中

c# - 推出 Beta 版时的 Google Play 游戏服务 : ERROR_NOT_AUTHORIZED,

android - 您的设备与此版本 Play 商店不兼容