Learn business growth with Google Analytics 4 Forums Google Analytics 4 Methods for sending access token to Google Analytics 4 Admin API

  • Methods for sending access token to Google Analytics 4 Admin API

    Posted by Olivia on 30 January 2023 at 5:50 am

    So, I’ve been playing around with the PHP library for the Google Analytics 4 admin API. The issue that I’ve stumbled upon is that I can’t seem to get the accounts list, and I think it’s because I’m not correctly passing the auth access_token. I’m not really sure about how to do that though. To make it clearer, here’s the piece of code I’ve been using:

    require 'vendor/autoload.php';
    
    use GoogleAnalyticsAdminV1alphaAnalyticsAdminServiceClient;
    use GoogleAnalyticsDataV1betaBetaAnalyticsDataClient;
    use GoogleAnalyticsDataV1betaDateRange;
    use GoogleAnalyticsDataV1betaDimension;
    use GoogleAnalyticsDataV1betaMetric;
    
    putenv('GOOGLE_APPLICATION_CREDENTIALS=config.json');
    
    $access_token = "**********";
    $client = new AnalyticsAdminServiceClient(['access_token' => $access_token]); // is this the correct way to pass the token?
    $accounts = $client->listAccountSummaries();
    

    I’m kind of stuck on figuring out the right way to use the access token with this API. Anyone who could guide me through this?

    Also, just for additional context this is what my JSON file looks like:

    {
        "type": "service_account",
        "project_id": "**********************",
        "private_key_id": "**********************",
        "private_key": "**********************",
        "client_email": "**********************.iam.gserviceaccount.com",
        "client_id": "**********************",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://oauth2.googleapis.com/token",
        "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
        "client_x509_cert_url": "*******************"
      }
    
    Morgan replied 12 months ago 3 Members · 2 Replies
  • 2 Replies
  • Elijah

    Member
    6 July 2023 at 12:29 pm

    Here are the steps you need to follow:

    First, you don’t need to worry about manually setting the access token. The Google PHP library has got you covered. It’s smart enough to fetch the token on its own. For this, you just need to point it to the service account JSON using the ‘GOOGLE_APPLICATION_CREDENTIALS’ environment variable.

    So, let’s simplify your code a bit. Include the ‘vendor/autoload.php’ file and use the ‘AnalyticsAdminServiceClient’ class from the PHP library.

    `php
    require ‘vendor/autoload.php’;
    use GoogleAnalyticsAdminV1alphaAnalyticsAdminServiceClient;
    `

    Next, use the ‘putenv’ function to set the path to your service account JSON file:

    `php
    putenv(‘GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service-account.json’);
    `

    Now, just create an instance of the ‘AnalyticsAdminServiceClient’ class. You don’t need to pass any access token here.

    `php
    $client = new AnalyticsAdminServiceClient();
    `

    Once the client is set, you can then fetch the list of accounts like so:

    `php
    $accounts = $client->listAccounts();
    `

    And finally, you can loop through these accounts and print their names:

    `php
    foreach ($accounts as $account) {
    print ‘Found account: ‘ . $account->getName() . PHP_EOL;
    }
    `

    And voilà! That’s pretty much it. Now you should be able to fetch the list of accounts without any issues. For more details, you can check the PHP Analytics Admin Samples on GitHub. Enjoy your coding experience!

  • Morgan

    Member
    8 July 2023 at 1:58 pm

    For authentication purposes, you don’t inherently need to manually pass an access token when using Google APIs on the server. The JSON config file that you have is used to auto-authenticate your client. The PHP client library for Google APIs uses a service account key file (JSON) referenced in the GOOGLE_APPLICATION_CREDENTIALS environment variable to automatically handle the OAuth 2.0 process. When you pass this environment variable, the client library takes care of generating the access token behind the scenes and using it in subsequent API calls. In your case, make sure your service account has the necessary permissions in Google Analytics and that the path in the GOOGLE_APPLICATION_CREDENTIALS variable correctly points to your JSON key file.

Log in to reply.