我在Apache上开发了一个OSGI包。该包公开不同的API以管理YouTube事件活动。捆绑服务将通过REST服务公开,用户将使用web浏览器(chrome、safari、mozilla)。
我为该帐户(client_secret和client_id)生成凭据google并将其保存在一个文件中,然后我的代码使用此凭据并正常工作。
我使用这个类(在youtube文档中找到)进行身份验证:
public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {
// Load client secrets.
Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);
// Checks that the defaults have been replaced (Default = "Enter X here").
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
LOGGER.info(
"Enter Client ID and Secret from https://console.developers.google.com/project/_/apiui/credential "
+ "into src/main/resources/client_secrets.json");
System.exit(1);
}
// This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore)
.build();
// Build the local server and bind it to port 8080
LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
// Authorize.
return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("myUsername");
}在日志上第一次使用时,我发现如下:
请在浏览器中打开以下地址:type=code&scope=https://www.googleapis.com/auth/youtube
除非我没有在浏览器中键入这个url,否则身份验证进程就被阻止了。键入并单击后,该过程工作正常。
我的问题是:
当用户在身份验证过程中使用浏览器调用此服务(api将包装此服务)时,在输入和调用url type=code&scope=https://www.googleapis.com/auth/youtube之前,如何通知客户端?
我希望对客户端进行透明的身份验证。身份验证过程必须只涉及服务器,确切地说是OSGI包。
提前感谢
发布于 2017-03-10 16:06:09
最后,我用这种方式解决了问题:
我在身份验证过程中添加了2个参数(在GoogleAuthorizationCodeFlow对象中):
.setAccessType("offline")
.setApprovalPrompt("force")然后只需要第一次用户确认,令牌将从API流中自动刷新(如果是过期的话)
https://stackoverflow.com/questions/42693219
复制相似问题