質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Q&A

0回答

1757閲覧

custom search APIとdaabの組み合わせ

snowman

総合スコア25

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

0グッド

1クリップ

投稿2018/07/13 07:51

編集2022/01/12 10:55

daabを使いseとdirectに送信すると検索結果が返ってくるようにしようとしています。
一応、他のAPIを使用した時のを参考に色々と書き換えてみたのですがうまくいきません。
アドバイスをいただけないでしょうか?

node.js

1'use strict'; 2 3const fs = require('fs'); 4const readline = require('readline'); 5const {google} = require('googleapis'); 6 7 8const SCOPES = ['https://www.googleapis.com/auth/CustomSearch.readonly']; 9const TOKEN_PATH = 'credentials.json'; 10 11module.exports = (robot) => { 12 robot.respond(/SE$/i, (res) => { 13 // Load client secrets from a local file. 14 try { 15 const content = fs.readFileSync('client_secret.json'); 16 authorize(JSON.parse(content), (auth) => { 17 const search = google.customsearch('v1'); 18 process.on('unhandledRejection', console.dir); 19 const res = search.cse.list({ 20 q: ,//検索ワード 21 apiKey: ,//APIキー 22 cx: //custom searchのやつ 23 }, (err, {date}) => { 24 if (err) return console.log('The API returned an error: ' + err); 25 26 const events = data.items; 27 var result = ""; 28 events.map((event, i) => { 29 const start = event.start.dateTime || event.start.date; 30 result = result + `${start} - ${event.summary}\n`; 31 }); 32 res.send(result); 33 }); 34 }); 35 } catch (err) { 36 return console.log('Error loading client secret file:', err); 37 } 38 }); 39}; 40 41 42/** 43 * Create an OAuth2 client with the given credentials, and then execute the 44 * given callback function. 45 * @param {Object} credentials The authorization client credentials. 46 * @param {function} callback The callback to call with the 47 “authorized client. 48 * @return {function} if error in reading credentials.json asks for a new one. 49 */ 50 function authorize(credentials, callback) { 51 const {client_secret, client_id, redirect_uris} = credentials.installed; 52 let token = {}; 53 const oAuth2Client = new google.auth.OAuth2( 54 client_id, client_secret, redirect_uris[0]); 55 56 // Check if we have previously stored a token. 57 try { 58 token = fs.readFileSync(TOKEN_PATH); 59 } catch (err) { 60 return getAccessToken(oAuth2Client, callback); 61 } 62 oAuth2Client.setCredentials(JSON.parse(token)); 63 callback(oAuth2Client); 64} 65 66/** 67 * Get and store new token after prompting for user authorization, and then 68 * execute the given callback with the authorized OAuth2 client. 69 * @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for. 70 * @param {getEventsCallback} callback The callback for the authorized client. 71 */ 72function getAccessToken(oAuth2Client, callback) { 73 const authUrl = oAuth2Client.generateAuthUrl({ 74 access_type: 'offline', 75 scope: SCOPES, 76 }); 77 console.log('Authorize this app by visiting this url:', authUrl); 78 const rl = readline.createInterface({ 79 input: process.stdin, 80 output: process.stdout, 81 }); 82 rl.question('Enter the code from that page here: ', (code) => { 83 rl.close(); 84 oAuth2Client.getToken(code, (err, token) => { 85 if (err) return callback(err); 86 oAuth2Client.setCredentials(token); 87 // Store the token to disk for later program executions 88 try { 89 fs.writeFileSync(TOKEN_PATH, JSON.stringify(token)); 90 console.log('Token stored to', TOKEN_PATH); 91 } catch (err) { 92 console.error(err); 93 } 94 callback(oAuth2Client); 95 }); 96 }); 97}

以下エラー文になります。

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問