2016-09-01 7 views
0

初めてStoredCredentialファイルを保存せずにアプリケーションを実行すると、プログラムがブラウザを開くことができません。その後、ブラウザを手動で開き、印刷されたリンクにアクセスして、アカウントにアクセスできるように大丈夫にしたら、プログラムは実行されません。プログラムを実行するたびに、実際にはこれが実行されます。gmail apiでブラウザが開かない

プログラムは毎回(リンクのみの変更)をこのような何かをプリントアウト:

2016-08-31 22:15:53.250:INFO::Logging to STDERR via org.mortbay.log.StdErrLog 
2016-08-31 22:15:53.250:INFO::jetty-6.1.26 
2016-08-31 22:15:53.258:INFO::Started [email protected]:35268 
Please open the following address in your browser: 
    https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=723296789344-l7b6jp5ffkmeteokur8qmi5fd8rkont5.apps.googleusercontent.com&redirect_uri=http://localhost:35268/Callback&response_type=code&scope=https://www.googleapis.com/auth/gmail.labels%20https://www.googleapis.com/auth/gmail.compose%20https://www.googleapis.com/auth/gmail.modify 
Attempting to open that address in the default browser now... 

私は与えられたリンクが自動的に開かないと、私はそれを行う際に、手動で、私のアカウントへのアクセスを受け入れ、言ったように、プログラムは実行を継続しません。何が起こっているのでしょうか?また、役に立つと思われる場合は、デフォルトブラウザが正しく設定された状態でUbuntu 16.04を使用しています。

また、OAuthフレームワークでアカウント認証を実装する場所はどこですか?

答えて

0

これは、クラスGoogleAuthorizationCodeFlow資格情報オブジェクトによって処理されると思います。 Java Quickstart for Gmailでは、これが実装されているかを確認することができます

public static Credential authorize() throws IOException { 
    // Load client secrets. 
    InputStream in = 
    GmailQuickstart.class.getResourceAsStream("/client_secret.json"); 
    GoogleClientSecrets clientSecrets = 
    GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 

    // Build flow and trigger user authorization request. 
    GoogleAuthorizationCodeFlow flow = 
    new GoogleAuthorizationCodeFlow.Builder(
    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 
    .setDataStoreFactory(DATA_STORE_FACTORY) 
    .setAccessType("offline") 
    .build(); 
    Credential credential = new AuthorizationCodeInstalledApp(
    flow, new LocalServerReceiver()).authorize("user"); 
    System.out.println(
    "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); 
    return credential; 
    } 

ここではいくつかの注目すべきは、GmailでおよそHow authorization worksです:

ハイレベルでは、すべてのアプリケーションは、同じ基本的な認証パターンは、次のとおりです。

1開発中は、Google APIコンソールでアプリケーションを登録します。

2.アプリが起動したら、ユーザーにGoogleアカウントのデータへのアクセスを許可するようにリクエストします。

3.ユーザーが同意すると、アプリケーションはGmail APIにアクセスするための資格情報を要求し、受信します。

4.資格情報を更新します(必要な場合)。

関連する問題