在我的网络服务中,我必须验证用户是否拥有youtube频道。
在我的Web服务中,我具有以下信息:
我想获取不带OAuth2的Google帐户的Youtube频道,因为我将在后端进行操作,因此无法向用户显示GoogleOAuth2同意屏幕。
我检查了documentation,但是所有通道方法都使用OAuth2。
如果我将idToken与list (my channel)
方法一起使用,那就太完美了。
这是我的授权码:
@Component
@RequiredArgsConstructor
public class YoutubeDataApiVerifier {
private static final String CLIENT_SECRETS= "client_secret.json";
private static final Collection<String> SCOPES =
Arrays.asList("https://www.googleapis.com/auth/youtube.readonly");
private static final String APPLICATION_NAME = "Fame Things";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/**
* Create an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public Credential authorize(final NetHttpTransport httpTransport, GoogleIdToken idToken, String token) throws Exception {
File file = ResourceUtils.getFile("classpath:" + CLIENT_SECRETS);
InputStream in = new FileInputStream(file);
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
.build();
/**
* ***************************************************************
* This line prints a Google OAuth2 Consent Screen to the console.
* But the user already authorized at client app and I send googleId token to the backend.
* It should give me the credentials without consent screen but I am missing something.
*/
Credential credential =
new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(token);
return credential;
}
/**
* Build and return an authorized API client service.
*
* @return an authorized API client service
* @throws GeneralSecurityException, IOException
*/
public YouTube getService(GoogleIdToken idToken, String token) throws Exception {
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Credential credential = authorize(httpTransport, idToken, token);
return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
public String getMyChannel(GoogleIdToken idToken, String token) throws Exception {
YouTube youtubeService = getService(idToken, token);
// Define and execute the API request
YouTube.Channels.List request = youtubeService.channels()
.list("snippet,contentDetails,statistics");
ChannelListResponse response = request.setMine(true).execute();
return response.toPrettyString();
}
}
问题出在这一行:
Credential credential =
new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(token);
我在后端有GoogleIdToken,可以用它获取凭据吗?