问题
I am using the following code for Authorizes the installed application to access user's protected data.
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;
}
What alternative code for above as method setCredentialStore get deprecated.
回答1:
adding from previous answer:
If you want to specify the filename, you can use DataStore<StoredCredential> dataStore = dataStoreFactory.getDataStore("specificFilename");
,then in GoogleAuthorizationCodeFlow
use the .setCredentialDataStore(dataStore)
method.
回答2:
Use FileDataStoreFactory fdsf = new FileDataStoreFactory(String whereToSave)
and then in your GoogleAuthorizationCodeFlow
use the .setDataStoreFactory(fdsf)
method.
Any subsequent launch this method will automatically check the location you specified for the storedCredential file and use that to give you the Credential you want.
来源:https://stackoverflow.com/questions/20144375/alternative-to-setcredentialstore-method-for-google-analytics