2

请检查我下面的代码,加 api 的 dart 代码,我遇到授权问题,我在 API 控制台中启用了 Google+ API。我正在使用 API 密钥而不是 OAuth2。

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

代码:

#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));
}

更新:嗨 Seth,从 jj 的回复中我了解到我还需要使用 oauth2,我能够从 JavaScript 成功测试以下内容。试图在 Dart 中复制,还没有成功:-(

  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);
      });
    });
  }
4

1 回答 1

2

您需要使用 API 密钥OAuth2。API 密钥识别您作为开发人员的身份。它不识别应用程序的用户是谁。

于 2012-08-27T05:22:31.763 回答