3

クライアント側でユーザーによる手動認証なしで、サーバーから動画をアップロードしようとしています。動画のアップロード用に以下のコード スニペットを試しましたが、ブラウザでユーザーを認証し、アプリの承認を求めます。

var ResumableUpload = require('node-youtube-resumable-upload');
var googleauth = require('google-auth-cli');
var google = require('googleapis');

var getTokens = function(callback) {
  googleauth({
      access_type: 'offline',
      scope: 'https://www.googleapis.com/auth/youtube.upload' //can do just 'youtube', but 'youtube.upload' is more restrictive
  },
  {     client_id: CLIENT_ID, //replace with your client_id and _secret
      client_secret: CLIENT_SECRET,
      port: 3000
  },
  function(err, authClient, tokens) {
    console.log(tokens);
    callback(tokens);
  });
};

getTokens(function(result) {
  tokens = result;
  upload();
});



var upload = function() {
      var metadata = {snippet: { title: 'title', description: 'Uploaded with ResumableUpload' },
          status: { privacyStatus: 'public' }};
      var resumableUpload = new ResumableUpload(); //create new ResumableUpload
      resumableUpload.tokens = tokens;
      resumableUpload.filepath = 'youtube/test4.mp4';
      resumableUpload.metadata = metadata;
      resumableUpload.monitor = true;
    resumableUpload.eventEmitter.on('progress', function(progress) {
        console.log(progress);
    });
      resumableUpload.initUpload(function(result) {
        console.log(result);
        return;
      });
    }

しかし、私のアプリでは、ビデオをサーバーから youtube に直接アップロードする必要があります。そのためには、アクセス トークンとリフレッシュ トークンが必要です。アクセス トークンを直接取得するために多くのことを試みましたが、取得できませんでした。

したがって、動画をサーバーからチャンネル アカウントに直接アップロードする方法についてのヘルプやアイデアはありません。それを行うためのノードモジュールをグーグルでたくさん検索しましたが、見つけることができませんでした。

私はこのアプローチを使用してビデオをアップロードしています

  1. クライアント ライブラリを使用して、Web ベースで生成されたトークンを取得します。
  2. アプリケーションと access_type=offline のユーザーから YouTube アップロード許可を取得しています。
  3. アクセス タイプ オフラインは、応答でリフレッシュ トークンを提供します。このトークンは、有効期限が切れたときにバックエンド サーバー トークンからのアップロードを続行するのに役立ちます。
  4. 許可を取ってから。コード付きの URL にリダイレクトします。
  5. 指定されたコードを使用して access_token を生成します
  6. 将来の使用のためにこのトークンを保存します。
  7. 同じトークンを使用して、サーバーから YouTube サーバーに動画をプッシュします
  8. 有効期限が切れたらトークンを更新します。

しかし、私のアプリケーションのユーザーからYouTubeのアップロード許可を取得せずに、このアプローチを実装する方法はありますか.

4

1 に答える 1

15

「サービスアカウント」でGoogle API(JWT)を利用してサーバーサイド認証を行うことができます。ただし、ユーザーの許可なしにサーバーから YouTube サーバーに直接アップロードすることはできません。動画をアップロードするには、Google に OAuth2.0 認証が必要です。JWT認証を使用した「サービスアカウント」で エラーunAuthorized(401)- youtubeSignupRequiredが表示されます。youtube会員登録必須

上記の制限のため。以下のアプローチを使用して、これを操作します-

  1. クライアント ライブラリを使用して、Web ベースで生成されたトークンを取得します。
  2. アプリケーションと access_type=offline のユーザーから YouTube アップロード許可を取得します。
  3. アクセス タイプ オフラインは、応答でリフレッシュ トークンを提供します。このトークンは、有効期限が切れたときにバックエンド サーバー トークンからのアップロードを続行するのに役立ちます。
  4. 許可を取ってから。コード付きの URL にリダイレクトします。
  5. 指定されたコードを使用して access_token を生成します
  6. 将来の使用のためにこのトークンを保存します。
  7. 同じトークンを使用して、サーバーから YouTube サーバーに動画をプッシュします
  8. 有効期限が切れたらトークンを更新します。手順 3 ~ 5 を再度実行します。
  9. 現在、これが YouTube に動画をアップロードする唯一の方法です。
  10. git リポジトリnodejs-upload-youtube-video-using-google-apiにコードを追加しました

なぜそれが不可能なのですか?以下の参照リンクとコードを確認してください。

  1. Google API Doc より:このエラーは、OAuth 2.0 サービス アカウント フローを使用しようとするとよく見られます。YouTube はサービス アカウントをサポートしていません。サービス アカウントを使用して認証しようとすると、このエラーが発生します。次のリンクを使用して、すべてのエラー コードとその詳細を確認できます: YouTube Data API - Errors
  2. gadata の問題から: Youtube v3 Google サービス アカウントへのアクセス
  3. Google ブログ スポットから:サービス アカウントを使用してサポートされている Google API のリスト
  4. サーバー側から access_token を取得するには、以下のコードを確認してください
  5. 以下の手順とコードを使用して、自分で確認できます。

    • Google デベロッパー コンソールに移動
    • プロジェクトを作成
    • Google+ APIアクセスを取得するには、[APIs & Auth] -> [APIs] -> [YouTube Data API v3 を有効にする] に移動します。
    • サービス アカウントを有効にするには、[APIs & Auth] -> [Credentials] -> [Create new Client ID] -> [Create new Client ID] -> [Service Account] -> [Create Client Id] をクリックします。
    • シークレット ファイルをシステムに保存し、安全に保管してください。
    • 以下のコマンドとダウンロードしたファイルを使用して秘密鍵を作成します。

openssl pkcs12 -in /home/rajesh/Downloads/Yourkeyfile.p12 -out youtube.pem -nodes

- Enter password: ***notasecret***

6. 以下のように、サーバー側から API を承認してアクセスできます。

    var google = require('googleapis');
    var authClient = new google.auth.JWT(
            'Service account client email address', #You will get "Email address" in developer console for Service Account:
            'youtube.pem', #path to pem file which we create using step 6
            null,
            ['https://www.googleapis.com/auth/youtube.upload'],
            null
    );
    authClient.authorize(function(err, tokens) {
       if (err) {
               console.log(err);
               return;
       }
       console.log(tokens);
    });
  1. サービス アカウント (作業中) を使用して YouTube ビデオ リストを取得します。

         var google = require('googleapis');
         var youtube = google.youtube('v3');
         var authClient = new google.auth.JWT(
              'Service account client email address', #You will get "Email address" in developer console for Service Account:
              'youtube.pem',
              null,
           ['https://www.googleapis.com/auth/youtube','https://www.googleapis.com/auth/youtube.upload'],
           null
         );
        authClient.authorize(function(err, tokens) {
            if (err) {
               console.log(err);
               return;
        }
        youtube.videos.list({auth:authClient,part:'snippet',chart:'mostPopular'}, function(err, resp) {
           console.log(resp);
           console.log(err);
         });
        });
    
  2. サービス アカウントと googleapis モジュールを使用して YouTube ビデオを挿入します。

         var google = require('googleapis');
         var youtube = google.youtube('v3');
         var authClient = new google.auth.JWT(
              'Service account client email address', #You will get "Email address" in developer console for Service Account:
              'youtube.pem',
              null,
           ['https://www.googleapis.com/auth/youtube','https://www.googleapis.com/auth/youtube.upload'],
           null
         );
        authClient.authorize(function(err, tokens) {
            if (err) {
               console.log(err);
               return;
        }
          youtube.videos.insert({auth:authClient,part:'snippet,status,contentDetails'},function(err,resp)
           console.log(resp);
           console.log(err);
         });
        });
    

挿入/アップロード API が次のエラーで返されました:

{ errors: 
   [ { domain: 'youtube.header',
       reason: 'youtubeSignupRequired',
       message: 'Unauthorized',
       locationType: 'header',
       location: 'Authorization' } ],
  code: 401,
  message: 'Unauthorized' }
  1. サービス アカウントと ResumableUpload モジュールを使用して YouTube ビデオを挿入します。

         var google = require('googleapis');
         var ResumableUpload = require('node-youtube-resumable-upload');
         var authClient = new google.auth.JWT(
              'Service account client email address', #You will get "Email address" in developer console for Service Account:
              'youtube.pem',
              null,
           ['https://www.googleapis.com/auth/youtube','https://www.googleapis.com/auth/youtube.upload'],
           null
         );
        authClient.authorize(function(err, tokens) {
            if (err) {
               console.log(err);
               return;
        }
          var metadata = {snippet: { title: 'title', description: 'Uploaded with ResumableUpload' },status: { privacyStatus: 'private' }};
          var resumableUpload = new ResumableUpload(); //create new ResumableUpload
          resumableUpload.tokens = tokens;
          resumableUpload.filepath = 'youtube.3gp';
          resumableUpload.metadata = metadata;
          resumableUpload.monitor = true;
          resumableUpload.eventEmitter.on('progress', function(progress) {
               console.log(progress);
          });
          resumableUpload.initUpload(function(result) {
               console.log(result);
               return;
          });
    
        });
    

挿入/アップロード API が次のエラーで返されました:

   { 'www-authenticate': 'Bearer realm="https://accounts.google.com/AuthSubRequest", error=invalid_token',
  'content-type': 'application/json; charset=UTF-8',
  'content-length': '255',
  date: 'Tue, 16 Sep 2014 10:21:53 GMT',
  server: 'UploadServer ("Built on Aug 18 2014 11:58:36 (1408388316)")',
  'alternate-protocol': '443:quic,p=0.002' }
  1. 「Googleキーの入手方法」のスクリーンショット添付 クライアント ID の作成 アプリケーションキーとパスワードをダウンロード バックエンドを使用して呼び出すために使用されるクライアントの電子メール ID

結論:ユーザーの許可なしに動画をアップロードすることはできません。

于 2014-09-16T09:19:10.593 に答える