2017-03-09 9 views
0

Apache FelixでOSGI Bundleを開発しました。このバンドルには、YouTubeイベントのライブを管理するための別のAPIが公開されています。バンドルサービスはRESTサービスを介して公開され、Webブラウザ(chrome、safari、mozilla)を持つユーザーによって使用されます。YouTube API - Oauth2 Flow(OSGI Bundle)

アカウント(client_secretとclient_id)の資格情報googleを生成してファイルに保存した後、私のコードはこの資格情報を使用して正常に動作します。

私は認証のために(ユーチューブドキュメントで見つかった)このクラスを使用:ログの最初の使用時に

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

のブラウザで次のアドレスを開いてください

私はこのURLをブラウザに入力しない限り、認証プロセスはブロックされました。タイプしてクリックした後、プロセスは正常に動作します。

私の質問は次のとおりです。

ユーザーが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は、クライアントがどのように、入力されたと呼ばれるまで呼び出しは、認証の過程でのブロッキング(APIのRESTがこのサービスをラップします)、ブラウザからこのサービスを呼び出す使用する場合通知された?

クライアントに透過的な認証が必要です。認証プロセスは、サーバー、正確にはOSGIバンドルのみを必要とします。予め

答えて

0

おかげで最後に私はこの方法で解決:初回のみが必要とされるその後

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

Iは、(GoogleAuthorizationCodeFlowオブジェクト内の)認証プロセスへの2つのパラメータを追加しましたユーザーが確認し、トークンはAPIフローから自動的にリフレッシュされます(有効期限が切れている場合)

関連する問題