gpt4 book ai didi

java - 如何使用 Google Sign in 授权 Youtube Data api 请求?

转载 作者:行者123 更新时间:2023-12-02 01:58:51 29 4
gpt4 key购买 nike

我正在使用 Google 登录对我的用户进行身份验证和授权使用我的应用。现在我想使用 Youtube Data api 发出 api 请求来提取用户可能拥有的 channel 列表。

在文档中,它说我需要一个用于“Authorizaiton:Bearer”的 OAuth token https://developers.google.com/youtube/v3/guides/auth/installed-apps#callinganapi

如何使用我通过 Google Sign 登录的帐户并获取所需的 OAuth token ?

更新:在这里尝试了答案:Authorize youtube data api request using google sign in api然而,它不起作用。在跟踪一个类和一些包之后,我放置的单独线程似乎挂起并且不返回任何内容。

Google 登录生成器

    AccessToken accessToken = AccessToken.getCurrentAccessToken();
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
Log.d("frag","login status" + String.valueOf(isLoggedIn));

scope = new Scope("https://www.googleapis.com/auth/yt-analytics.readonly");
scopeTwo = new Scope("https://www.googleapis.com/auth/youtube.readonly");
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(scope,scopeTwo)
// .requestIdToken(getString(R.string.server_client_id))
.requestServerAuthCode(getString(R.string.server_client_id))
.build();

googleSignInClient = GoogleSignIn.getClient(getActivity(), gso);

运行 Google Sign 和 API 的 Intent

            api = new GoogleApiClient.Builder(getContext())
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
api.connect();


Intent signInIntent = googleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, 21);

Intent intent = Auth.GoogleSignInApi.getSignInIntent(api);
startActivityForResult(intent,115);

最佳答案

看到这个... https://developers.google.com/android/reference/com/google/android/gms/auth/GoogleAuthUtil

https://developers.google.com/api-client-library/java/google-oauth-java-client/reference/1.20.0/com/google/api/client/auth/oauth2/Credential

第 1 步.获取用户电子邮件

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
if (resultCode == RESULT_OK) {
email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
request();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "You must pick an account", Toast.LENGTH_SHORT).show();
//finish();
}
}
super.onActivityResult(requestCode, resultCode, data);
}

protected void pickUserAccount() {
String[] accountTypes = new String[]{"com.google"};
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
accountTypes, true, null, null, null, null);

try {
startActivityForResult(intent, 1001);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Your phone seems to be missing Google Play Services.", Toast.LENGTH_SHORT).show();
}
}

第2步.获取凭据

public String getTokenWithEmailOnly(Context context, String email){
String token = null;

try {
token = GoogleAuthUtil.getToken(context, email, "oauth2:https://www.googleapis.com/auth/youtube");

TokenResponse tokenResponse = new TokenResponse();
tokenResponse.setAccessToken(token);
Credential credential = createCredentialWithAccessTokenOnly(tokenResponse);
YouTube youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), credential).build();

} catch (Throwable t) {

}

return token;
}

private Credential createCredentialWithAccessTokenOnly(TokenResponse tokenResponse) {
return (new Credential(BearerToken.authorizationHeaderAccessMethod())).setFromTokenResponse(
tokenResponse);
}

关于java - 如何使用 Google Sign in 授权 Youtube Data api 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57385820/

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