首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >YouTube API - Oauth2流(Oauth2束)

YouTube API - Oauth2流(Oauth2束)
EN

Stack Overflow用户
提问于 2017-03-09 10:44:22
回答 1查看 351关注 0票数 0

我在Apache上开发了一个OSGI包。该包公开不同的API以管理YouTube事件活动。捆绑服务将通过REST服务公开,用户将使用web浏览器(chrome、safari、mozilla)。

我为该帐户(client_secret和client_id)生成凭据google并将其保存在一个文件中,然后我的代码使用此凭据并正常工作。

我使用这个类(在youtube文档中找到)进行身份验证:

代码语言:javascript
复制
    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包。

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-10 16:06:09

最后,我用这种方式解决了问题:

我在身份验证过程中添加了2个参数(在GoogleAuthorizationCodeFlow对象中):

代码语言:javascript
复制
.setAccessType("offline")
.setApprovalPrompt("force")

然后只需要第一次用户确认,令牌将从API流中自动刷新(如果是过期的话)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42693219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档