2017-05-22 47 views
0

我正在使用谷歌驅動器認證使用Oauth2.0和驅動器V2 API。我已經爲我在Google應用程序控制臺中新創建的應用程序獲取了clientId,客戶端密鑰和重定向URI。我試圖使用這些clientID和客戶端密鑰來驗證驅動器。 以下是我用於谷歌驅動器驗證並從中獲取文件的代碼。谷歌驅動器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我指定和谷歌帳戶權限,以允許訪問。一旦完成身份驗證,我將上傳一些文件來驅動。我希望這種身份驗證是持久性的,以便我可以在後臺執行這些操作。但我認爲每3600秒我都會獲得一個新窗口以允許訪問。是否有任何方法可以解決此問題?

回答

0

變化setAccessType變爲offline。這將導致訪問令牌和刷新令牌。理論上,庫會自動使用RT來獲取新的AT,因爲它需要它們。

相關問題