2

我将使用 Nodejs google api 客户端(google-api-nodejs-client)将照片发布到我的 google+。(我在这篇文章的末尾列出了我所有的代码。)

让我介绍一下背景:

  • 我在以下位置创建了一个项目:console.developers.google.com

  • 我已经为这个项目启用了 google+ 域 API。

  • 我也为这个项目创建了凭据。(它是 OAuth 2.0 客户端 ID)

  • 我有一点使用客户端(google-api-nodejs-client)的经验,我可以通过它将图像和文件发布到我的谷歌驱动器。

但是,发布到 google+ 照片是不同的,授权是不同的关键。我尝试了几种不同的方法,但没有一个有效。

api总是给我这个:

{ [Error: Forbidden]
  code: 403,
  errors: [ { domain: 'global', reason: 'forbidden', message: 'Forbidden' } ]     }

我还发现了这个:

警告:Google+ 登录使用的 Google+ 登录按钮和 plus.login 范围目前不支持与 Google+ 域 API 一起使用。使用授予 www.googleapis.com/auth/plus.login 范围的身份验证令牌向 Google+ Domains API 发出的请求或由 Google+ 登录按钮生成的请求将失败。

如果它不支持签名按钮,它支持什么?

此页面告诉我添加域委托(https://developers.google.com/+/domains/authentication/delegation),但我没有将我的程序推送到任何服务器,我只是尝试在本地运行它。

我想知道是否可以通过在本地运行 nodejs 程序来使用此客户端将照片发布到 google+?

var CLIENT_ID = "xxxxxxx.apps.googleusercontent.com";
var CLIENT_SECRET = "xxxxxxxx";
var REDIRECT_URL = "https://xxxxxxx";

var readline = require('readline');
var async = require('async');
var google = require('googleapis');
var request = require('request');

var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

function getAccessToken (oauth2Client, callback) {
  // generate consent page url
    var scopes = [
    'https://www.googleapis.com/auth/plus.me',
    'https://www.googleapis.com/auth/plus.stream.read',
    'https://www.googleapis.com/auth/plus.stream.write',
    'https://www.googleapis.com/auth/plus.circles.read',
    'https://www.googleapis.com/auth/plus.circles.write',
    'https://www.googleapis.com/auth/plus.media.upload'
    ];

    var url = oauth2Client.generateAuthUrl({
      access_type: 'offline', // 'online' (default) or 'offline' (gets refresh_token)
      scope: scopes, // If you only need one scope you can pass it as string,
      key: 'p7UALH460Deqodhvb2zESYya'
    });

  console.log('Visit the url: ', url);
  rl.question('Enter the code here:', function (code) {
    // request access token
    oauth2Client.getToken(code, function (err, tokens) {
      if (err) {
        return callback(err);
      }
      // set tokens to the client
      // TODO: tokens should be set by OAuth2 client.
      oauth2Client.setCredentials(tokens);
      console.dir(tokens);

      callback();
    });
  });
}

getAccessToken(oauth2Client, function () {

    var plusDomains = google.plusDomains({ version: 'v1', auth: oauth2Client });

    var requestObj = request({url:'http://asset1.cxnmarksandspencer.com/is/image/mands/2643f540b32fe8c6cccdec95b3a2c5239166232f?$editorial_430x430$'});
    const Readable = require('stream').Readable;
    var iamgeStream = new Readable().wrap(requestObj);

    plusDomains.media.insert({
      userId: 'me',
      collection: 'cloud',
      resource: {
        name: 'testimage.png',
        mimeType: 'image/png'
      },
      media: {
        mimeType: 'image/png',
        body: iamgeStream 
      },
      access:{domainRestricted :"true"}
    }, callbackFn);

    function callbackFn(argument) {
        console.dir(argument);
    }

});

非常感谢你!彼得

4

0 回答 0