Alternative to setCredentialStore method for google analytics

泪湿孤枕 提交于 2019-12-22 10:19:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!