gpt4 book ai didi

android - 如何知道用户在 android 的 youtube api v2 中接受 oauth 权限

转载 作者:太空狗 更新时间:2023-10-29 15:11:08 27 4
gpt4 key购买 nike

我想在 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

最佳答案

我认为本教程很有用:

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

}

关于android - 如何知道用户在 android 的 youtube api v2 中接受 oauth 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16212925/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com