2

Nodejs Google API クライアント (google-api-nodejs-client) を使用して、Google+ に写真を投稿します。(この投稿の最後にすべてのコードをリストしました。)

少し背景を紹介しましょう:

  • console.developers.google.com でプロジェクトを作成しました。

  • このプロジェクトで google+ ドメイン API を有効にしました。

  • このプロジェクトの資格情報も作成しました。(OAuth 2.0 クライアント ID です)

  • 私はクライアント (google-api-nodejs-client) の使用経験が少しあり、Google ドライブに画像やファイルを投稿できます。

ただし、google+ photo への投稿は別で、認証がキーとなります。いくつかの方法を試しましたが、どれもうまくいきません。

APIは常にこれを返します:

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

これも見つけました:

警告: Google+ サインイン ボタンと Google+ サインインで使用される plus.login スコープは、現在、Google+ Domains 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