我想在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
答案 0 :(得分:0)
我认为本教程非常有用:
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);
}
}