My Business
3.7K members online now
3.7K members online now
For developers who are using the Google My Business API to manage locations
Guide Me
star_border
Reply

Error 401 Request does not have valid authentication credentials

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hello,

 

I'm using a web server and would like to communicate server to server. I followed the code from here:

https://www.en.advertisercommunity.com/t5/Google-My-Business-API/Error-401-The-request-does-not-have...

 

I downloaded the correct JSON file for the OATH2 client ID and i've checked my credentials with the OAUTH2 playground (it works there), but i'm still getting (401) - The request does not have valid authentication credentials.

 

Is this error thrown while trying to get the authorization code the first time? If so, I may be using the wrong credential type? The email associated with the approved Google My Business API is < Text has been removed to protect private information >

 

thanks for your help!

 

 

 

//////////////////////////// USING LARAVEL FRAMEWORK. GoogleAPI and My_Business is imported in above
        define('APPLICATION_NAME', 'User Query - Google My Business API');
        define('CREDENTIALS_PATH', '../credentials.json');
        define('CLIENT_SECRET_PATH', 'client_secret_773406822823-k6escaf7dn7oiq4jpc7ulo03rgugd6u1.apps.googleusercontent.com.json');

        $redirect_uri = 'http://homestead.app/gb';

        $client = new Google_Client();
        $client->setApplicationName(APPLICATION_NAME);
        $client->setAuthConfigFile(CLIENT_SECRET_PATH);
        $client->addScope("https://www.googleapis.com/auth/plus.business.manage");
        $client->setRedirectUri($redirect_uri);

// For retrieving the refresh token
        $client->setAccessType("offline");
        $client->setApprovalPrompt("force");

        /************************************************
        We are going to create the Google My Business API
        service, and query it.
         ************************************************/
        $mybusinessService = new Google_Service_Mybusiness($client);

        $credentialsPath = CREDENTIALS_PATH;
        if (isset($_GET['code'])) {
            // Exchange authorization code for an access token.
            $accessToken = $client->authenticate($_GET['code']);
            // Store the credentials to disk.
            if (!file_exists(dirname($credentialsPath))) {
                mkdir(dirname($credentialsPath), 0700, true);
            }
            file_put_contents($credentialsPath, $accessToken);
            $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
            header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
        }

// Load previously authorized credentials from a file.
        if (file_exists($credentialsPath)) {
            $accessToken = file_get_contents($credentialsPath);
            $client->setAccessToken($accessToken);
            // Refresh the token if it's expired.
            if ($client->isAccessTokenExpired()) {
                $client->refreshToken($client->getRefreshToken());
                file_put_contents($credentialsPath, $client->getAccessToken());
            }
        } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
        }
        $accounts = $mybusinessService->accounts->listAccounts()->getAccounts();
        return $accounts; 

 

1 Expert replyverified_user

Re: Error 401 Request does not have valid authentication credentials

Google Employee
# 2
Google Employee

Hi @Andrew I,

 

Your source code looks good to me, but is your web server able to use OAuth 2.0 to authorize requests via a browser? For web application credentials, did you choose, set and check the correct redirect URI (e.g. http://homestead.app/gb) to request user consent for your web server application to work? Please note that you can also build a PHP command-line application and persist the refresh token with the Google API Client Library for PHP by following the code snippet in this thread. However, when you create an OAuth 2.0 client ID through the Google Developers Console for a command-line application, you should specify that this is an Installed application and select "Other" as the application type.

 

In addition, you should write your code to anticipate the possibility that a granted token might no longer work. Please delete the credentials file and obtain a new token when the existing token is deemed invalid.

 

Thanks,

Terry

Re: Error 401 Request does not have valid authentication credentials

[ Edited ]
Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

 

Hi Terry,

 

I found that my code wasn't returning the authorization url, once i did that, i was able to get the needed token. Can you check to see if my account is correct? I'm only seeing an unverified personal account. But i should be part of a verified business one. (unless that's just a verified business location).

 

thanks,

-Andrew

 

Re: Error 401 Request does not have valid authentication credentials

Google Employee
# 4
Google Employee

Hi @Andrew I,

 

If I understand you correctly, you have successfully been authenticated and obtained an OAuth 2.0 access token as well as a refresh token for your application to access the Google My Business API in a consistent manner without having to request user consent again.

 

The UNVERIFIED account status you see in your personal account indicates that your account is not bulk verified. If you think it’s necessary to verify if your account contains the correct verified business location, you can provide me with either the account_name of your personal account/business account that contains that location or the locationId of that specific location and I’ll confirm it with you using our internal system by giving you the business name and placeId of the location.

 

Please let me know how I can assist you.

 

Thanks,

Terry

Re: Error 401 Request does not have valid authentication credentials

[ Edited ]
Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

Hi Terry,

Yes, i have successfully authenticated and have a refresh token without requesting user consent again.

What do you recommend? We have a few thousand global locations we would like to start claiming and managing programmatically. Would it be ok to associate them with my personal account? Or should a generic new google account be created? Right now, i'm just working on a proof of concept. But eventually we would like to manage all these locations and their related Google Plus pages using our own systems.

 

Also, is it possible to get access to the pages api to post? I saw a sign-up form https://developers.google.com/+/web/api/rest/pages-signup but we're not a social media company, we just have a lot of locations to manage. I saw there was https://developers.google.com/+/domains/ but i'm not sure if that would fit us, as we don't have a domain registered with google. What would be the best way to go about this?

thanks,
-Andrew

Re: Error 401 Request does not have valid authentication credentials

Google Employee
# 6
Google Employee

Hi @Andrew I,

 

We recommend using business accounts to share access to a large set of locations with a group of people within a Google Account. Please check out the best practices for using Business Accounts with Google My Business locations in the FAQ. However, please note the 100-page limit per Google Account when using business accounts. Since you will manage a few thousand locations of the same chain, you may request bulk verification, which will remove the 100-page limit for that account.

 

You should check out the Google+ Platform Developers site to find out which tool is best for your development. Access is available through a whitelist for the Google+ Pages API which allows social media management companies to add Google+ page management features into their tool. On the other hand, the Google+ Domains API lets you develop tools to interact with Google+ features such as posts, comments, and circles. Please find help on the Google+ Support Options page if you have any more questions related to the Google+ Platform.

 

Thanks,

Terry