我正在尝试使用twitters Premium Search API搜索var verifyPermission = new Promise(function(resolve, reject) {
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
console.log('Notify.js -> Service Worker and Push is supported');
var dataUrl = document.getElementById('directUrl').value;
Notification.requestPermission(status => {
isNotificationAllow = status;
console.log('Notify.js -> Notification permission status:', status);
if(status === 'denied'){
reject(null);
}
navigator.serviceWorker.getRegistrations().then(registrations => {
if(registrations.length == 0){
navigator.serviceWorker.register(dataUrl)
.then(swReg => {
console.log('Notify.js -> Service Worker is registered', swReg);
if(swReg.active){
resolve(swReg);
// this gets failed swReg.active.postMessage(..)
}
else{
console.log("Not yet registered 1");
}
})
.catch(err => {
console.error('Notify.js -> Service Worker Error', err);
});
}else{
console.log("Notify.js -> Already registered service worker ",registrations[0]);
if(registrations[0].active)
resolve(registrations[0]);
else{
console.log("Not yet registered 2");
}
}
});
});
});
}else{
reject(null);
//alert('Notify.js -> Service Worker and Push is NOT supported');
}
});
(twitters强制性URL缩短)URL。使用标准搜索API可以获得结果,但是使用高级API会遇到麻烦。
我一直在使用t.co
npm软件包。
twitter
...这就是调用标准搜索api的方法:
const Twitter = require('twitter')
const client = new Twitter({
consumer_key: '...',
consumer_secret: '...',
access_token_key: '...',
access_token_secret: '...'
})
...但是使用高级搜索API失败
client.get('search/tweets', { q: 'https://t.co/ qwjhdxDp9z' }, function(error, tweets, response) {
console.log(tweets)
})
我假设由于url编码而失败,但是如果我不对url进行编码,则会返回此错误:
消息:“处理您的请求时出错:不可行 字符\'/ \'的替代项(在位置7),没有可行的替代项 在字符\'/ \'处(在位置8)
使用上述方法,搜索正常的字符串,例如let uri = 'https://t.co/ qwjhdxDp9z'
let res = encodeURIComponent(uri)
client.get('tweets/search/fullarchive/prod', { query: uri, fromDate: '200603220000', toDate: '201910141338' }, function(error, tweets, response) {
console.log(tweets)
})
可以正常工作,只是在搜索url时出现问题。
banana
更新: 为了构建上述搜索查询,我在这里使用Twitter搜索API文档中的信息:https://developer.twitter.com/en/docs/tweets/search/api-reference/premium-search