2017-05-22 21 views
0

のための永続的な認証を駆動します。私はclientId、クライアントシークレットとリダイレクトURIを私の新しく作成されたアプリケーションのGoogleアプリコンソールで持っている。私はこれらのclientIDとクライアントの秘密を使用してドライブでautheticateしようとしている。 Googleドライブで認証してファイルを取得するために使用したコードは次のとおりです。Googleは私がOauth2.0を使用してGoogleドライブの認証とドライブV2 APIで働いているWebアプリケーション

public class GoolgeDriveUpload3 { 

    private static String CLIENT_ID = "xxxxxxxxxx"; 
    private static String CLIENT_SECRET = "yyyyyyyyyy"; 
    static HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); 
    static JsonFactory jsonFactory = new JacksonFactory(); 
    private static FileDataStoreFactory DATA_STORE_FACTORY; 

    private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), 
      ".credentials/drive-java-quickstart"); 

    static { 
     try { 
      HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 
      DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); 
     } catch (Throwable t) { 
      t.printStackTrace(); 
      System.exit(1); 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, jsonFactory, 
       CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE_FILE)).setDataStoreFactory(DATA_STORE_FACTORY) 
         .setAccessType("online").setApprovalPrompt("auto").build(); 
     Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalCallbackServer()).authorize("user"); 
     Drive service = new Drive.Builder(HTTP_TRANSPORT, jsonFactory, credential).build(); 
     List<File> result = new ArrayList<File>(); 
     Files.List request = null; 
     request = service.files().list(); 
     FileList files = request.setQ("'root' in parents and trashed=false ").execute(); 
     result.addAll(files.getItems()); 
     request.setPageToken(files.getNextPageToken()); 

     for (File f : result) { 
      System.out.println("Files are: " + f.getTitle() + " " + f.getId() + " " + f.getAlternateLink()); 
     } 
    } 
    } 

public class LocalCallbackServer implements VerificationCodeReceiver { 

    volatile String code; 
    private final int LOCAL_SERVER_PORT = 9058; 

    @Override 
    public synchronized String waitForCode() { 

     try { 
      this.wait(); 
     } catch (Exception ex) { 
     } 
     System.out.println("returning code is -> " + code); 
     return code; 

    } 

    @Override 
    public String getRedirectUri() { 

     new Thread(new MyThread()).start(); 
     return "http://127.0.0.1:" + LOCAL_SERVER_PORT; 
    } 

    @Override 
    public void stop() { 
    } 

    class MyThread implements Runnable { 

     @Override 
     public void run() { 
      try { 
       // return GoogleOAuthConstants.OOB_REDIRECT_URI; 
       ServerSocket ss = new ServerSocket(LOCAL_SERVER_PORT); 
       System.out.println("server is ready..."); 
       Socket socket = ss.accept(); 
       System.out.println("new request...."); 
       InputStream is = socket.getInputStream(); 
       StringWriter writer = new StringWriter(); 
       String firstLine = null; 

       InputStreamReader isr = new InputStreamReader(is); 
       StringBuilder sb = new StringBuilder(); 
       BufferedReader br = new BufferedReader(isr); 
       String read = br.readLine(); 
       firstLine = read; 
       OutputStream os = socket.getOutputStream(); 
       PrintWriter out = new PrintWriter(os, true); 

       StringTokenizer st = new StringTokenizer(firstLine, " "); 
       st.nextToken(); 
       String codeLine = st.nextToken(); 
       st = new StringTokenizer(codeLine, "="); 
       st.nextToken(); 
       code = st.nextToken(); 

       out.write("RETURNED CODE IS " + code + ""); 
       out.flush(); 
       // is.close(); 

       socket.close(); 

       System.out.println("Extracted coded is " + code); 

       synchronized (LocalCallbackServer.this) { 
        LocalCallbackServer.this.notify(); 
       } 
       System.out.println("return is " + sb.toString()); 

      } catch (IOException ex) { 
       Logger.getLogger(LocalCallbackServer.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
} 

私はこれを実行すると、ブラウザでURIを指定してGoogleアカウントのアクセス許可を許可した新しいウィンドウが表示されます。認証が完了すると、私はドライブにいくつかのファイルをアップロードします。私はバックグラウンドでこれらのアクションを実行できるように、このaunthenticationを永続化します。しかし、私は3600秒ごとに私はアクセスを許可するための新しいウィンドウが表示されていると思います。私はこの問題を解決できる方法はありますか? offline

答えて

0

変更setAccessType。これにより、アクセストークンとリフレッシュトークンの両方が発生します。理論的には、ライブラリは自動的にRTを使用して、必要に応じて新しいATをフェッチします。

0

あなたは後に、それを再利用するためのローカルストレージにCredentialオブジェクトを保存する必要があります。資格のオブジェクトが用に作成日または無制限の場合(リフレッシュアクセストークンを使用して)使用ACCESS_TYPE =「オフライン」(セクション「アクセストークンを更新(オフラインアクセス)」https://developers.google.com/identity/protocols/OAuth2WebServerの)

関連する問題