3

我想在android中使用youtube api v2, 当我在webview中打开url时,按照developers.google.com/youtubehttps://accounts.google.com/o/oauth2/auth?client_id=client_id&redirect_uri=redirect_uri&scope=https://gdata.youtube.com&response_type=code,它要求访问应用程序。但我怎么知道用户是否接受它,因为在接受 webview 重定向到另一个页面后我可以得到success code=4/fsgko348949

4

1 回答 1

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);
    }

}
于 2013-04-26T10:26:26.050 回答