【问题标题】:how to know that user accept oauth permission in youtube api v2 in android如何知道用户在 android 的 youtube api v2 中接受 oauth 权限
【发布时间】:2013-04-25 10:57:47
【问题描述】:
我想在 android 中使用 youtube api v2,遵循 developers.google.com/youtube
当我在 webview 中打开 https://accounts.google.com/o/oauth2/auth?client_id=client_id&redirect_uri=redirect_uri&scope=https://gdata.youtube.com&response_type=code url 时,它要求访问应用程序。但是我怎么知道用户是否接受它,因为在接受 webview 重定向到另一个页面后,我可以获得success code=4/fsgko348949
【问题讨论】:
标签:
android
youtube-api
android-webview
【解决方案1】:
我认为这个教程很有用:
http://blog.blundell-apps.com/tut-oauth-youtube-api-access/
本教程展示了如何使用侦听器检测用户是否选择了“允许访问”:
package com.blundell.youtubesignin.oauth;
/**
* This class checks what paramater's our redirect url has informing our listener
* it also passes the auth code if access is granted.
*
* Google Documentation:
* If the user granted access to your application,
* Google will have appended a code parameter to the redirect_uri.
* This value is a temporary authorization code that you can exchange for an access token.
* example : http://localhost/oauth2callback?code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf
*
* If the user refused to grant access to your application,
* Google will have included the access_denied error message in the hash fragment of the redirect_uri.
* example : http://localhost/oauth2callback#error=access_denied
*
* @author paul.blundell
*
*/
public class ParamChecker implements OnOAuthCallbackListener {
private final OnOAuthListener onOAuth;
public ParamChecker(OnOAuthListener onOAuth) {
this.onOAuth = onOAuth;
}
@Override
public void onOAuthCallback(String params) {
if(params.contains("access_denied")){
// User said no
onOAuth.onRefused();
} else {
// User auth'd us
String authCode = extractAuthCode(params);
onOAuth.onAuthorized(authCode);
}
}
private static String extractAuthCode(String params) {
return params.substring(Constants.AUTH_CODE_PARAM.length()+1);
}
}