2013-11-22 45 views
3

我正在使用以下代碼來授權已安裝的應用程序訪問用戶的受保護數據。谷歌分析的替代setCredentialStore方法

private Analytics iniAnalytics (String secureFolder) { 
     try { 
      HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
      JsonFactory jasonFactory = new JacksonFactory(); 
      /** Authorizes the installed application to access user's protected data. */ 
      GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
            jasonFactory, 
            new FileReader(secureFolder + "client_secrets.json")); 
      FileCredentialStore credentialStore = new FileCredentialStore(
        new File(secureFolder, "analytics.json"), 
        jasonFactory); 

      GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, 
            jasonFactory, 
            clientSecrets, 
            Collections.singleton(AnalyticsScopes.ANALYTICS_READONLY)) 

         .setCredentialStore(credentialStore).build(); 
      Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()) 
            .authorize("user"); 

      return new Analytics.Builder(httpTransport, jasonFactory, credential) 
      .setApplicationName("myapp/Analytics/2.0").build(); 
     } catch (GeneralSecurityException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 

以上針對方法setCredentialStore的替代代碼獲得了棄用。

回答

0

使用FileDataStoreFactory fdsf = new FileDataStoreFactory(String whereToSave)然後在您的GoogleAuthorizationCodeFlow使用.setDataStoreFactory(fdsf)方法。

任何後續啓動此方法將自動檢查您爲storedCredential文件指定的位置,並使用它爲您提供所需的憑證。

1

從以前的答案補充說:

如果要指定文件名,你可以使用DataStore<StoredCredential> dataStore = dataStoreFactory.getDataStore("specificFilename");,然後在GoogleAuthorizationCodeFlow使用.setCredentialDataStore(dataStore)方法。