私はyoutube analytics apiにリクエストをしようとしています。彼らが受け入れられるように要求を形成するいくつかの問題を抱えています。私は、Google APIのNode.jsのクライアントYoutube Analytics - リクエストの作成
https://github.com/google/google-api-nodejs-client
を使用していますし、
import { Meteor } from 'meteor/meteor';
import google from 'googleapis';
import KEY_FILE from './keyFile.json';
import { CHANNEL_ID } from './channelId.js';
//api's
const analytics = google.youtubeAnalytics('v1');
//fetch youtube analytics
export function youtubeAnalytics(start, end){
//initalise request data
const startDate = `${start.getFullYear()}-${('0'+(start.getMonth()+1)).slice(-2)}-${('0'+(start.getDate())).slice(-2)}`;
const endDate = `${end.getFullYear()}-${('0'+(end.getMonth()+1)).slice(-2)}-${('0'+(end.getDate())).slice(-2)}`;
const scopes = [
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/yt-analytics-monetary.readonly',
'https://www.googleapis.com/auth/yt-analytics-monetary.readonly'
];
//generate authorisation token
var AUTH = new google.auth.JWT(
KEY_FILE.client_email,
null,
KEY_FILE.private_key,
scopes,
null
);
//authorize request
AUTH.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
}
//create request
const analyticsRequest = {
auth: AUTH,
'start-date': startDate,
'end-date': endDate,
ids: `channel==${CHANNEL_ID}`,
metrics: 'views',
};
//make request
analytics.reports.query(analyticsRequest, function (err, data) {
if (err) {
console.error('Error: ' + err);
return false;
}
if (data) {
console.log(data);
return data;
}
});
});
return false;
}
Meteor.methods({youtubeAnalytics});
を次のように私のコードは私が
Error: Error: Invalid query. Query did not conform to the expectations.
は、私がその行うに考えて、次のエラーを取得しておくです私のリクエストオブジェクトで
const analyticsRequest = {
auth: AUTH,
'start-date': startDate,
'end-date': endDate,
ids: `channel==${CHANNEL_ID}`,
metrics: 'views',
};
しかし、私が見つけたすべての例では、このリクエストオブジェクトが機能するはずです。私はできるだけそれを単純化した。私の最初の要求(私が実際に欲しいもの)は次の通りです。
const analyticsRequest = {
auth: AUTH,
'start-date': startDate,
'end-date': endDate,
ids: `channel==${CHANNEL_ID}`,
metrics: 'views',
dimensions: 'video',
sort: '-views',
'max-results': '200'
}
その後、別のapiエンドポイントを使用する動画の関連情報をすべて取得するために別のリクエストを行う必要があります。
//api's
const youtube = google.youtube('v3');
/*
do processing of analytics data to create batchRequest
which is a string of comma separated video ids
*/
videoRequest = {
auth: AUTH,
part: 'id,snippet',
id: batchRequest;
}
youtubeApiData.search.list(videosRequest, function (err, data) {
if (err) {
console.error('Error: ' + err);
return false;
}
if (data) {
console.log(data);
return data;
}
});
のでまとめ
では、私は、GoogleのAPIの様々にリクエストを行う必要があり、それらが受け入れられているので、(私はユーチューブの分析から最初のリクエストを過ぎて持っていない)要求を形成するトラブルを抱えています。
誰かが正しい方向に向けることができますか?