2017-03-09 84 views
0

我在Apache Felix上开发OSGI Bundle。该套件公开了不同的API以实时管理YouTube事件。捆绑服务将通过REST服务公开,并由用户使用Web浏览器(chrome,safari,mozilla)。YouTube API - Oauth2 Flow(OSGI Bundle)

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

我使用这个类(在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"); 
} 

在对数第一次使用,我发现这一点:

请在浏览器中打开以下网址: https://accounts.google.com/o/oauth2/auth?client_id=my_client_id&redirect_uri=http://localhost:8080/Callback&response_type=code&scope=https://www.googleapis.com/auth/youtube

直到我没有在浏览器中输入此网址,身份验证过程被阻止。键入并单击后,该过程正常工作。

我的问题是:

当用户使用电话从浏览器这个服务(API REST将包裹这项服务)的调用在认证过程中阻塞,直到URL https://accounts.google.com/o/oauth2/auth?client_id=my_client_id&redirect_uri=http://localhost:8080/Callback&response_type=code&scope=https://www.googleapis.com/auth/youtube键入并呼吁,怎么客户端通知?

我想要一个透明的身份验证到客户端。身份验证过程只能涉及服务器,完全是OSGI捆绑包。

预先感谢

回答

0

最后,我以这种方式解决:

我加入2-参数来验证(在GoogleAuthorizationCodeFlow对象)过程:

.setAccessType("offline") 
.setApprovalPrompt("force") 

然后仅需要在第一时间用户确认,令牌将自动从API流程刷新(如果将过期)