【问题标题】:OAuth: App Id or redirect_uri does not match authorization codeOAuth:App Id 或 redirect_uri 与授权码不匹配
【发布时间】:2015-09-29 18:39:28
【问题描述】:
App Id or redirect_uri does not match authorization code.

由于我是 OAuth 和应用程序开发方面的菜鸟,我猜这个错误(大多数时候)在我这边。我的应用程序有一个按钮(登录),将用户引导到一个 web 视图,他通过 OAuth 登录 Misfit API (https://build.misfit.com/)。一旦他同意与我的应用程序共享他的 Misfit 数据,webview 想要将他重定向到我的 redirect_uri,但我总是收到上述错误消息。这是 OAuthActivity 的代码:

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 = "ID";
    public static String CLIENT_SECRET = "Secret";
    public static String CALLBACK_URL = "http://iss.uni-saarland.de/";
    public static String SCOPE = "public,birthday,email,tracking,session,sleeps";

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

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

        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 = "grant_type=authorization_code" + "&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&code=" + accessCode + "&redirect_uri=" + CALLBACK_URL;
                    view.postUrl(OAUTH_ACCESS_TOKEN_URL, query.getBytes());
                }
            }



        });
        webview.loadUrl(url);


    }
}

我知道这应该以某种方式劫持授权 URL 以获取访问码,如果不可用,请尝试获取令牌。有些人建议在它想把我带到我的 redirect_uri 之前中断活动,但我不知道该怎么做。

基于答案的其他信息: - Misfit 应用程序设置中注册的重定向 URI 是我在代码中使用的重定向 URI。 - 我为我的应用程序构建了一个 Intent 处理程序,以便在调用重定向 URI 时启动其主要活动。

============================================

在 REST 客户端中我得到了相同的结果 >>>>

POST: https://api.misfitwearables.com/auth/tokens/exchange

请求:

{
    "grant_type":"authorization_code",
    "code":{{USER CODE FROM AUTH}},
    "redirect_uri":"SAME REDIRECT_URI AS IN AUTH",
    "client_id":{{my app id}},
    "client_secret":{{my app secret}}

}

回应:

{
  "error": "invalid_grant",
  "error_description": "App Id or redirect_uri does not match authorization code"
}

【问题讨论】:

    标签: java android oauth misfit


    【解决方案1】:

    这有点猜测,但是当您无法在 google api 控制台中注册您的应用程序时,您可能会在 google 中看到类似的错误。

    如果您转到 misfits 控制台并为您的应用注册重定向 url,它应该可以工作:

    1. 登录 misfits 控制台
    2. 注册新应用或编辑现有应用。
    3. 确保应用程序域与您的重定向 URL 匹配

    我希望这会有所帮助。

    【讨论】:

    • 感谢您的回复,但我已经这样做了。这就是我想知道错误可能来自哪里的原因。
    猜你喜欢
    • 2019-05-03
    • 2019-04-01
    • 2019-05-11
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 2016-07-20
    • 2020-10-15
    • 2018-11-21
    相关资源
    最近更新 更多