使用 Google 登录以授权 YouTube 数据 API
Use Google Sign In to authorize YouTube Data API
我一直在关注 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 登录进行了身份验证,我如何使用 GoogleSignInAccount
对象创建一个 Credential
对象以传递给 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());
...
}
我一直在关注 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 登录进行了身份验证,我如何使用 GoogleSignInAccount
对象创建一个 Credential
对象以传递给 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());
...
}