作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我使用以下代码授权安装的应用程序访问用户的 protected 数据。
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 的替代代码被弃用了。
最佳答案
从之前的答案中添加:
如果要指定文件名,可以使用DataStore<StoredCredential> dataStore = dataStoreFactory.getDataStore("specificFilename");
, 然后在 GoogleAuthorizationCodeFlow
使用 .setCredentialDataStore(dataStore)
方法。
关于java - 谷歌分析的 setCredentialStore 方法的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20144375/
我使用以下代码授权安装的应用程序访问用户的 protected 数据。 private Analytics iniAnalytics (String secureFolder) { t
我是一名优秀的程序员,十分优秀!