gpt4 book ai didi

java - 使用Google登录授权YouTube数据API

转载 作者:行者123 更新时间:2023-12-03 06:15:28 24 4
gpt4 key购买 nike

我一直遵循this article将Google登录功能实现到Android应用中。

最后的代码为我们提供了一个GoogleSignInAccount对象,该对象包含有关已登录用户的信息:

private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
updateUI(true);
} else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}

在YouTube数据API的所有 code samples provided中,都使用以下身份验证代码:
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in = ApiExample.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader( in ));

// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}

现在,因为我已经使用Google登录进行了身份验证,如何使用 Credential对象创建 GoogleSignInAccount对象并传递给 YouTube.Builder 呢?

最佳答案

public static final Collection<String> YOUTUBE_SCOPE = Arrays.asList("https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtubepartner");

...

private void handleSignInResult(GoogleSignInResult result) {

...

GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, YOUTUBE_SCOPE);
credential.setSelectedAccount(acct.getAccount());

...

}

Source

关于java - 使用Google登录授权YouTube数据API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44488413/

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