使用Dart的Google Plus API - 状态401,授权问题

时间:2012-08-27 01:23:27

标签: authorization google-plus dart

请检查下面的代码,plus 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);
      });
    });
  }

1 个答案:

答案 0 :(得分:2)

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