2017-03-09 100 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流程刷新(如果將過期)