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

标签 authorization google-plus dart

请检查下面的代码,plus api 的 dart 代码,我遇到授权问题,我已在 API 控制台中启用了 Google+ API。我使用 API key 而不是 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);
      });
    });
  }

最佳答案

您需要使用 API key OAuth2。 API key 可识别您作为开发人员的身份。它无法识别应用程序的用户是谁。

关于authorization - 使用 Dart 的 Google Plus API - 状态 401,授权问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12135314/

相关文章:

Flutter网站网址导航

php - 使用 PHP 连接、授权和获取 Web 应用程序的数据

firebase - Firestore - 授予对子集合的访问权限也会授予对父集合的访问权限?

hadoop - 限制 Hadoop 中的文件夹访问

javascript - 如何在 cordova/ionic 应用程序中放置 Google Plus +1 按钮?

firebase - 将来自 Firebase 的 StartAt 与 Flutter 时间戳一起使用

c# - 使用 ClaimsPrincipalPermission 和 ADFS 2.0 返回的声明来注释类

ios - 如何使用 GIDSignIn 或 GIDAuthentication 刷新 authentication.idToken?

php - 如何使用php自动发布到google+

image - Dart-获取ImageElement的像素颜色