daabを使いseとdirectに送信すると検索結果が返ってくるようにしようとしています。
一応、他のAPIを使用した時のを参考に色々と書き換えてみたのですがうまくいきません。
アドバイスをいただけないでしょうか?
'use strict';
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/CustomSearch.readonly'];
const TOKEN_PATH = 'credentials.json';
module.exports = (robot) => {
robot.respond(/SE$/i, (res) => {
// Load client secrets from a local file.
try {
const content = fs.readFileSync('client_secret.json');
authorize(JSON.parse(content), (auth) => {
const search = google.customsearch('v1');
process.on('unhandledRejection', console.dir);
const res = search.cse.list({
q: ,//検索ワード
apiKey: ,//APIキー
cx: //custom searchのやつ
}, (err, {date}) => {
if (err) return console.log('The API returned an error: ' + err);
const events = data.items;
var result = "";
events.map((event, i) => {
const start = event.start.dateTime || event.start.date;
result = result + `${start} - ${event.summary}\n`;
});
res.send(result);
});
});
} catch (err) {
return console.log('Error loading client secret file:', err);
}
});
};
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the
“authorized client.
* @return {function} if error in reading credentials.json asks for a new one.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
let token = {};
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
try {
token = fs.readFileSync(TOKEN_PATH);
} catch (err) {
return getAccessToken(oAuth2Client, callback);
}
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return callback(err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
try {
fs.writeFileSync(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to', TOKEN_PATH);
} catch (err) {
console.error(err);
}
callback(oAuth2Client);
});
});
}
以下エラー文になります。
TypeError: Cannot destructure 'undefined' or 'null'.
at search.cse.list (/Users/ne280042/Desktop/プログラミング/node/searchchat/mybot/scripts/ping.js:23:12)
at /Users/ne280042/Desktop/プログラミング/node/searchchat/mybot/node_modules/google-auth-library/build/src/transporters.js:77:17
at <anonymous>:null:null
at process._tickCallback (internal/process/next_tick.js:188:7)
error: restarting script because change changed
error: Forever detected script was killed by signal: SIGKILL
error: Script restart attempt #4
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
まだ回答がついていません
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 90.36%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる