zomdar
zomdar

Reputation: 273

Authenticating Google Analytics without a web browser in JAVA

I currently have a Google analytics program to extract some analytic data from my website. Currently it sits on my local machine and is working fine. However I would like to move the program into our Oracle Cloud server, which hosts my web files. It's a linux based server with little to no browser support. I've tried running the program on the server but as usual, it prompts me to go to a website to authenticate Oauth..ect.ect

(https://accounts.google.com/o/oauth2/auth?client_id=658818029220-701kg25os6dqq1ua577cpn8drujjuvl8.apps.googleusercontent.com&redirect_uri=http://localhost:23100/Callback&response_type=code&scope=https://www.googleapis.com/auth/analytics.readonly)

I've tried using a web browser on the server...does not work too well. I was wondering if anyone had solutions that'll allow me to use this application without having to use a web browser to authenticate.

EDIT: Having a hard time with the Service Acc thing

So... I have this in my current application to get the auth going...

     /** Authorizes the installed application to access user's protected data. */
  private static Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
        JSON_FACTORY, new InputStreamReader(
            extractanalyticsgoogleapi.class.getResourceAsStream("/client_secrets.json")));
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
        || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
      System.out.println(
          "Enter Client ID and Secret from https://code.google.com/apis/console/?api=analytics "
          + "into analytics-cmdline-sample/src/main/resources/client_secrets.json");
      System.exit(1);
    }
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        httpTransport, JSON_FACTORY, clientSecrets,
        Collections.singleton(AnalyticsScopes.ANALYTICS_READONLY)).setDataStoreFactory(
        dataStoreFactory).build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
  }

And i'm not sure as to where i would put hte service Acc code into

Upvotes: 1

Views: 1030

Answers (1)

Matt
Matt

Reputation: 5178

What you want to do is set up a service account that is authorized to access your Google Analytics data. This allows your server code to access your Google Analytics information.

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.analytics.AnalyticsScopes;

// ...

String emailAddress = "[email protected]";
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(httpTransport)
    .setJsonFactory(JSON_FACTORY)
    .setServiceAccountId(emailAddress)
    .setServiceAccountPrivateKeyFromP12File(new File("MyProject.p12"))
    .setServiceAccountScopes(Collections.singleton(AnalyticsScopes.ANALYTICS_READONLY))
    .build();

Upvotes: 2

Related Questions