darth_vader
darth_vader

Reputation: 189

Unable to authorize using Google api client to access Hangout chat api

I am using google.api.client version 1.30.9 . I want to authenticate my requests using Hangout Chat Api before sending . In the official doc https://developers.google.com/api-client-library/java/google-api-java-client/oauth2 google has used GoogleCredential but it has now been deprecated. I am using the following code to get the authenticated credential object that can be used to build HangoutChat object.

private static Credential authorize() throws Exception {
        httpTransport=GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
        // load client secrets
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                new InputStreamReader(HangoutsChat.class.getResourceAsStream("/client_secrets.json")));
        // set up authorization code flow
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, JSON_FACTORY, clientSecrets,
                Collections.singleton(HangoutsChat.DEFAULT_BASE_URL)).setDataStoreFactory(dataStoreFactory)
                .build();
        // authorize
        return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");

    }

AuthorizationCodeInstalledApp and LocalServerReceiver and are not available in the latest version of google API . How can I authenticate before requesting using hangout chat API.

Upvotes: 0

Views: 953

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

You state that you are trying to use a service account but the code you are using was designed fora web application to login using Oauth2 this will notwork with service account credentials you must use the proper code with the corect type of credentials.

service account

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
...
// Build service account credential.

GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("MyProject-1234.json"))
    .createScoped(Collections.singleton(PlusScopes.PLUS_ME));
// Set up global Plus instance.
plus = new Plus.Builder(httpTransport, jsonFactory, credential)
    .setApplicationName(APPLICATION_NAME).build();
...

Explicit Credential Loading

Google auth libray

To get Credentials from a Service Account JSON key use GoogleCredentials.fromStream(InputStream) or GoogleCredentials.fromStream(InputStream, HttpTransportFactory). Note that the credentials must be refreshed before the access token is available.

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/path/to/credentials.json"));
credentials.refreshIfExpired();
AccessToken token = credentials.getAccessToken();
// OR
AccessToken token = credentials.refreshAccessToken();

Upvotes: 1

Related Questions