私は、Googleカスタム検索エンジンとGoogle APIキーを使用してレスポンスを返し、レスポンスボディから結果を説明するURL、タイトル、テキストスニペットを抽出します。結果は得られますが、結果にitems配列は含まれず、結果のsearchInformationのtotalResults = 0:インターネット上に猫の画像がなければなりません。戻り値は、app.routeメソッドのHTTP動詞の末尾にある中括弧と括弧の直前です。Googleカスタム検索とgoogle apiキーを使用したGoogle画像検索では返されない結果が返される
この質問は調査されており、StackOverflowでも同様の質問がありますが、回答はありません。質問は「GOOGLE-のAPI-エクスプローラ」タグ
app.route('/')
.get(function(req, res) {
var endPoint = 'https://www.googleapis.com/customsearch/v1?key=' + API_KEY + '&cx=' +
CSE_ID + '&q=cats&num=10&searchType=image&start=1'
https.get(endPoint, (response) => {
const statusCode = response.statusCode;
const contentType = response.headers['content-type'];
var error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error('Invalid content-type.\n' +
`Expected application/json but received ${contentType}`);
}
if (error) {
console.error(error.message);
// consume responseponse data to free up memory
response.resume();
return;
}
response.setEncoding('utf8');
var rawData = '';
response.on('data', (chunk) => { rawData += chunk;
});
response.on('end',() => {
try {
res.send(JSON.parse(rawData));
} catch (e) {
console.error(e.message);
}
});
}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
});
//res.sendFile(process.cwd() + '/views/index.html');
})
// what it returns.
{
"kind":"customsearch#search",
"url":{
"type":"application/json",
"template":"https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
},
"queries":{
"request":[
{
"title":"Google Custom Search - lectures",
"totalResults":"0",
"searchTerms":"cats",
"count":10,
"startIndex":1,
"inputEncoding":"utf8",
"outputEncoding":"utf8",
"safe":"off",
"cx":"013162268872379598895:76khcwygut4",
"searchType":"image"
}
]
},
"searchInformation":{
"searchTime":0.340305,
"formattedSearchTime":"0.34",
"totalResults":"0",
"formattedTotalResults":"0"
}
}
返信いただきありがとうございます。画像検索が有効になった – Evan
悪いあなたが正しい。画像検索を有効にする必要があります。私はこの問題を解決しようとしてきたように思う。長い間、私の脳はもはや正しく働いていない – Evan