App Id or redirect_uri does not match authorization code.
由于我在OAuth和应用程序开发方面是新手,所以我想错误(大多数情况下)可能是我的问题。我的应用程序有一个按钮(登录),它将用户引导到一个webview,在那里他通过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以获取访问代码,如果不可用,则尝试获取令牌。一些人建议在活动要将我带到重定向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"
}