【问题标题】:OAuth: Missing parameter response_typeOAuth:缺少参数 response_type
【发布时间】:2015-09-29 11:31:40
【问题描述】:

我正在构建一个连接到 Misfit 的 API 以收集数据并进行一些科学研究的移动 android 应用程序。 (https://build.misfit.com/) Misfit 的 API 使用了一种 OAuth 授权方法,证明有点困难。

我按下按钮会打开一个 WebView 到 Misfit 的授权页面,然后我可以在其中登录。登录后,webview 会产生以下错误:

{"error":"invalid_request","error_description":"Missing required parameter: response_type"}

我发出该请求的代码如下: 我们的想法是发布令牌并获取访问代码,将它们都存储在 SharedPreferences 中,这样并不是每个应用程序启动都需要新的登录

public class OAuthActivity extends Activity {

public static String OAUTH_URL = "https://api.misfitwearables.com/auth/dialog/authorize";
public static String OAUTH_ACCESS_TOKEN_URL = "https://api.misfitwearables.com/auth/tokens/exchange";

public static String CLIENT_ID = "Here's a client ID";
public static String CLIENT_SECRET = "and the secret, that obviously stays hidden.";
public static String CALLBACK_URL = "http://localhost";

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

    String url = OAUTH_URL + "?client_id=" + CLIENT_ID;

    WebView webview = (WebView)findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    final SharedPreferences prefs = this.getSharedPreferences(
            "com.iss_fitness.myapplication", Context.MODE_PRIVATE);
    webview.setWebViewClient(new WebViewClient() {
        public void onPageStarted(WebView view, String URL, Bitmap favicon) {
            String accessTokenFragment = "access_token=";
            String accessCodeFragment = "code=";

            // We hijack the GET request to extract the OAuth parameters

            if (url.contains(accessTokenFragment)) {
                // the GET request contains directly the token
                String accessToken = url.substring(url.indexOf(accessTokenFragment));
                prefs.edit().putString("Token", accessToken);

            } else if(url.contains(accessCodeFragment)) {
                // the GET request contains an authorization code
                String accessCode = url.substring(url.indexOf(accessCodeFragment));
                prefs.edit().putString("Code", accessCode);


                String query = "&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&code=" + accessCode;
                view.postUrl(OAUTH_ACCESS_TOKEN_URL, query.getBytes());
            }

        }



    });
    webview.loadUrl(url);


}

注意:我在网上找到了这段代码,它是我作为一个新的应用程序开发人员最容易理解的代码之一。尽管如此,仍然没有给出任何解释,如果上面的代码被证明是错误的(或我对它的理解),请纠正我。另外:登录后如何获得将我重定向到主要活动的 OAuth 活动?

【问题讨论】:

    标签: java android oauth misfit


    【解决方案1】:

    您在授权和令牌请求中都缺少一些请求参数。将身份验证网址更改为:

    String url = String url = OAUTH_URL+ "?response_type=code" +"&client_id=" + CLIENT_ID+ "&redirect_uri=" + CALLBACK_URL + "&scope=" + SCOPE;
    

    其中 SCOPE 是逗号分隔的权限字符串,例如“public,birthday,email”。

    并且,将令牌请求参数更改为:

    String query = "grant_type=authorization_code" + "&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&code=" + accessCode+ "&redirect_uri=" + CALLBACK_URL;
    

    更多详情请参考Misfit api reference

    【讨论】:

    • 感谢您的回答,这正是我所缺少的。在你解释之后,我什至在 API 参考资料中找到了它。我想我现在要睡觉了。
    猜你喜欢
    • 2016-12-14
    • 2015-07-13
    • 2013-01-10
    • 2010-11-30
    • 1970-01-01
    • 2014-10-14
    • 2014-05-31
    • 2017-01-15
    • 2015-12-31
    相关资源
    最近更新 更多