Google Active DirectoryとGoogle Groupsの同期を実行するJavaアプリケーションを作成しました。夜間にクレーンによって実行されると考えられる非インタラクティブなアプリケーションです。 私のラップトップ(DEV環境)では完全に動作します。 私の問題は、最初の実行時に、ブラウザウィンドウにGoogle APIへのアクセスを許可するダイアログがポップアップすることです。 「許可する」ボタンをクリックすると、うまくいって終了します。 これをCentOSを実行し、ブラウザを持たないプロダクションサーバーに移動する必要があります。私はそれが次のメッセージ出力し、この環境で自分のアプリケーションを実行すると は:Google OAuth2との非インタラクティブ認証
Please open the following address in your browser:
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=520105804541-c7d7bfki88qr8dbkv6oahp22i3oq1jq0.apps.googleusercontent.com&redirect_uri=http://localhost:50089/Callback&response_type=code&scope=https://www.googleapis.com/auth/admin.directory.group.member%20https://www.googleapis.com/auth/admin.directory.orgunit%20https://www.googleapis.com/auth/admin.directory.device.mobile.action%20https://www.googleapis.com/auth/admin.directory.orgunit.readonly%20https://www.googleapis.com/auth/admin.directory.userschema%20https://www.googleapis.com/auth/admin.directory.device.chromeos%20https://www.googleapis.com/auth/admin.directory.notifications%20https://www.googleapis.com/auth/admin.directory.device.chromeos.readonly%20https://www.googleapis.com/auth/admin.directory.device.mobile%20https://www.googleapis.com/auth/admin.directory.group.readonly%20https://www.googleapis.com/auth/admin.directory.group.member.readonly%20https://www.googleapis.com/auth/admin.directory.userschema.readonly%20https://www.googleapis.com/auth/admin.directory.user.alias%20https://www.googleapis.com/auth/admin.directory.user.security%20https://www.googleapis.com/auth/admin.directory.device.mobile.readonly%20https://www.googleapis.com/auth/admin.directory.user%20https://www.googleapis.com/auth/admin.directory.user.readonly%20https://www.googleapis.com/auth/admin.directory.user.alias.readonly%20https://www.googleapis.com/auth/admin.directory.group%20https://www.googleapis.com/auth/apps.groups.settings
をこのマシンには何のブラウザはありませんが、私は別の箱にそれを実行しようとする場合、私は、エラー400取得しています - 無効な要求を。 理由は明らかです。なぜなら、localhost:50089にリダイレクトされ、誰もそのマシン上のそのポートでリッスンしていないからです。 developers.google.comで複数のドキュメントとサンプルを調べると、サービスアカウントを作成し、それにプライマリキーを生成することでダイアログをバイパスする方法が見つかりました。
try {
GoogleCredential cr = GoogleCredential
.fromStream(new FileInputStream(keyFile))
.createScoped(SCOPES);
Directory directory = new Directory.Builder(httpTransport, jsonFactory, cr)
.setApplicationName(config.getProperty("google.application.name")).build();
} catch (IOException ex) {
LOG.error("Failed to establish access credentials : ", ex.getMessage());
}
それがキーを読んず、Directoryインスタンスを作成しないが、それへの要求は403応答コードになり
{ "コード":403、 "エラー":[{ "ドメイン" : "global"、 "message": "このリソース/ apiにアクセスする権限がありません"、 "理由": "禁止"
}] "メッセージ": "このリソース/ apiにアクセスする権限がありません"} "
しかし私は、 lこのアカウントのサービスアカウントに対する権限。私が他に何ができるかわからない。 誰かがこのジャムから私を助けることができれば、私はそれを非常に感謝します。
あなたは命の恩人です! Googleドキュメントがうまくいきません。これはちょうど有効だった唯一のサンプルです。 – Alex