user1626554
user1626554

Reputation: 21

Google Plus API using Dart - status 401, authorization issue

Please check my code below, dart code for plus api, I am getting authorization issue, I have enabled Google+ API in API Console. I am using API key instead of OAuth2.

Failed to load resource: the server responded with a status of 401 (Unauthorized)

Code:

#import ('dart:html');
#import("package:google-api-dart-client/plus-v1.dart");
void main () {
  final request = new PlusApi();
  request.key = "I have entered API key here";
  Future<Person> myPerson=request.people.get("me");
  myPerson.then((user)=>print(user.id));
}

Update: Hi Seth, from jj's reply I understood that I also need to use oauth2, I was able to test following successfully from JavaScript. Trying to replicate in Dart, no success yet :-(

  var clientId = 'here clientId';
  var apiKey = 'here API Key';
  var scopes = 'https://www.googleapis.com/auth/plus.me';
  function handleClientLoad() {
    gapi.client.setApiKey(apiKey);
    window.setTimeout(checkAuth,1);
  }
  function checkAuth() {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
  }


  function handleAuthResult(authResult) {
    var authorizeButton = document.getElementById('authorize-button');
    if (authResult && !authResult.error) {
      authorizeButton.style.visibility = 'hidden';
      makeApiCall();
    } else {
      authorizeButton.style.visibility = '';
      authorizeButton.onclick = handleAuthClick;
    }
  }

  function handleAuthClick(event) {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
    return false;
  }

  function makeApiCall() {
    gapi.client.load('plus', 'v1', function() {
      var request = gapi.client.plus.people.get({
        'userId': 'me'
      });
      request.execute(function(resp) {
        var heading = document.createElement('h4');
        var image = document.createElement('img');
        image.src = resp.image.url;
        heading.appendChild(image);
        heading.appendChild(document.createTextNode(resp.displayName));

        document.getElementById('content').appendChild(heading);
      });
    });
  }

Upvotes: 2

Views: 1388

Answers (1)

Shannon -jj Behrens
Shannon -jj Behrens

Reputation: 5030

You need to use the API key and OAuth2. The API key identifies who you are as a developer. It doesn't identify who the user of the application is.

Upvotes: 2

Related Questions