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

gmb token

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

Hi there, I am using this library https://github.com/google/google-api-javascript-client to get a token and manage listings.

 

 

<script type="text/javascript">
         var apiKey = 'xxx';
         var clientId = 'yyy';
         var scopes = 'https://www.googleapis.com/auth/plus.business.manage';
         var auth2; // The Sign-In object.
         function handleClientLoad() {
             gapi.load('client:auth2', initAuth);
         }

         function initAuth() {
             gapi.client.setApiKey(apiKey);
             gapi.auth2.init({
                 client_id: clientId,
                 scope: scopes
             }).then(function () {
                 auth2 = gapi.auth2.getAuthInstance();
                 auth2.isSignedIn.listen(updateSigninStatus);
                 updateSigninStatus(auth2.isSignedIn.get());
                 authorizeButton.onclick = handleAuthClick;
                 signoutButton.onclick = handleSignoutClick;
             });
         }

         function updateSigninStatus(isSignedIn) {
             if (isSignedIn) {
                 authorizeButton.style.display = 'none';
                 signoutButton.style.display = 'block';
                 makeApiCall();
             } else {
                 authorizeButton.style.display = 'block';
                 signoutButton.style.display = 'none';
             }
         }

         function handleAuthClick(event) {
             auth2.signIn();
         }

         function handleSignoutClick(event) {
             auth2.signOut();
         }

         function makeApiCall() {
             /*gapi.client.request({
                 path: 'https://www.googleapis.com/auth/plus.business.manage'
             }).then(function(data){
                 console.log('data', data);
             })*/
             gapi.client.load('people', 'v1', function() {
                 var request = gapi.client.people.people.get({
                     resourceName: 'people/me'
                 });
                 request.execute(function(resp) {
                    console.log('resp', resp);
                 });
             });
         }
        </script>
        <script src="https://apis.google.com/js/api.js?onload=handleClientLoad"></script>

 

the important part of code is the makeApiCall function. where I need to get the token form gmb permmisons.

 

1 Expert replyverified_user
Marked as Best Answer.
Solution
Accepted by topic author Jesse A
October 2016

Re: gmb token

Google Employee
# 2
Google Employee

Hi @Jesse A,

 

The Google My Business API is not in the list of supported APIs and versions in the APIs Explorer for the Google API Client Library for JavaScript. Therefore, you may not be able to use the Google API Client Library for JavaScript entirely for the Google My Business API, and you should find help on its Support page. Please check out the Using OAuth 2.0 for Client-side Web Applications page for more information.

 

For your reference, you can use the code snippet below to make requests by sending the obtained access token to the Google My Business API in an HTTP authorization header with the Google API Client Library for JavaScript:

<!DOCTYPE html>

<html>

 <head>

   <title>Google My Business API</title>

   <meta charset='utf-8' />

 </head>

 <body>

   <p>Google My Business API Sample</p>

 

   <!--Add buttons to initiate auth sequence and sign out-->

   <button id="authorize-button" style="display: none;">Authorize</button>

   <button id="signout-button" style="display: none;">Sign Out</button>

 

   <div id="content"><span id="info"></span></div>

 

   <script type="text/javascript">

     // Enter an API key from the Google API Console:

     //   https://console.developers.google.com/apis/credentials?project=_

     var apiKey = 'YOUR_API_KEY';

 

     // Enter a client ID for a web application from the Google API Console:

     //   https://console.developers.google.com/apis/credentials?project=_

     // In your API Console project, add a JavaScript origin that corresponds

     //   to the domain where you will be running the script.

     var clientId = ‘YOUR CLIENT ID’;

 

     // Enter one or more authorization scopes. Refer to the documentation for

     // the API or https://developers.google.com/identity/protocols/googlescopes

     // for details.

     var scopes = 'https://www.googleapis.com/auth/plus.business.manage';

 

     var auth2; // The Sign-In object.

     var authorizeButton = document.getElementById('authorize-button');

     var signoutButton = document.getElementById('signout-button');

 

     function handleClientLoad() {

       // Load the API client and auth library

       gapi.load('client:auth2', initAuth);

     }

 

     function initAuth() {

       gapi.client.setApiKey(apiKey);

       gapi.auth2.init({

           client_id: clientId,

           scope: scopes

       }).then(function () {

         auth2 = gapi.auth2.getAuthInstance();

 

         // Listen for sign-in state changes.

         auth2.isSignedIn.listen(updateSigninStatus);

 

         // Handle the initial sign-in state.

         updateSigninStatus(auth2.isSignedIn.get());

 

         authorizeButton.onclick = handleAuthClick;

         signoutButton.onclick = handleSignoutClick;

       });

     }

 

     function updateSigninStatus(isSignedIn) {

       if (isSignedIn) {

         authorizeButton.style.display = 'none';

         signoutButton.style.display = 'block';

         makeApiCall();

       } else {

         authorizeButton.style.display = 'block';

         signoutButton.style.display = 'none';

       }

     }

 

     function handleAuthClick(event) {

       auth2.signIn();

     }

 

     function handleSignoutClick(event) {

       auth2.signOut();

     }

 

     // Load the API and make an API call.  Display the results on the screen.

     function makeApiCall() {

       var xhttp = new XMLHttpRequest();

       var uri = "https://mybusiness.googleapis.com/v3/accounts";

       xhttp.onreadystatechange = function() {

         if (this.readyState == 4 && this.status == 200) {

           // Typical action to be performed when the document is ready:

           var infoDiv = document.getElementById('info');

           infoDiv.innerHTML = '';

           infoDiv.appendChild(document.createElement("BR"));

           infoDiv.appendChild(document.createTextNode(xhttp.responseText));

           console.log(xhttp.responseText);

         }

       };

       xhttp.open("GET", uri, true);

       xhttp.setRequestHeader("Content-Type", "application/json");

       xhttp.setRequestHeader("Authorization", auth2.currentUser.get().getAuthResponse().token_type + " " + auth2.currentUser.get().getAuthResponse().access_token);

       xhttp.send();

     }

   </script>

   <script src="https://apis.google.com/js/api.js?onload=handleClientLoad"></script>

 </body>

</html>

 

Thanks,

Terry

gmb token

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Thanks !!! very useful.

 

gmb token

[ Edited ]
Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭

@Terry W This code snippet does not seem to work any longer. It consistently gives the error:

  1. details:"Not a valid origin for the client: http://localhost:8080 has not been whitelisted for client ID 666503920209-oodak************e2ulq95.apps.googleusercontent.com. Please go to https://console.developers.google.com/ and whitelist this origin for your project's client ID."
  2. error:"idpiframe_initialization_failed"

I have made sure that localhost:8080 is whitelisted. Any ideas?