【发布时间】: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"
}
【问题讨论】: