公式ドキュメントやこちらなどを参考に、glitchで干すディンクしているのですが、TwitterのActive Account APIのwebhookの登録がうまくいきません。
どうやってもNon-200 response code during CRC GET request (i.e. 404, 500, etc).
が返ってしまいます。しかしサーバー側にはログも残っていないためアクセスすらできてないように思えます。
手動でGETした場合の動作は問題ないように思えるのですが、何が原因なのでしょうか。
登録APIは、Inosomniaと、tweepyのAPI.request
で叩いてみています。
ログも残らないということはやはりURL自体を正しく渡せていないのでしょうか。
Inosomniaではhttps://api.twitter.com/1.1/account_activity/all/dev3/webhooks.json?url=https%3A%2F%2Ftwitter-aaapi-node.glitch.me%2Fwebhook
(Form URL Encodedに入れるとCould not authenticate youになる)、tweepyではendpoint='account_activity/all/dev3/webhooks', params={'url':'https://twitter-aaapi-test.glitch.me/webhook/'}
を指定しています。
エラーメッセージ
json
1{ 2 "errors": [ 3 { 4 "code": 32, 5 "message": "Could not authenticate you." 6 } 7 ] 8}
glitchリンク
ソースコード
python
1import os 2import base64 3import hashlib 4import hmac 5import json 6 7from flask import Flask, request, jsonify, make_response 8 9app = Flask(__name__) 10API_KEY = os.environ['API_KEY'] 11API_SECRET = os.environ['API_SECRET'] 12 13 14@app.route('/') 15def top(): 16 return 'hello' 17 18 19@app.route('/webhook/', methods=['GET']) 20def webhook_challenge(): 21 crc_token = request.args.get('crc_token', '') 22 digest = hmac.new(API_SECRET.encode(), crc_token.encode(), hashlib.sha256).digest() 23 content = {'response_token': 'sha256=' + base64.b64encode(digest).decode()} 24 return jsonify(content), 200 25 26 27@app.route('/webhook/', methods=['POST']) 28def webhook(): 29 response = make_response('', 200) 30 response.mimetype = "text/plain" 31 return response 32 33app.run(port=8000) 34
js
1const crypto = require('crypto'); 2const fastify = require('fastify')({ 3 logger: true 4}) 5 6fastify.get('/', async (req, reply) => { 7 reply.type('application/json').code(200) 8 return { hello: 'world' } 9}) 10 11fastify.get('/webhook', async (req, reply) => { 12 const API_SECRET = process.env['API_SECRET']; 13 const hmac = crypto.createHmac('sha256', process.env['API_SECRET']); 14 const digest = hmac.update(req.query.crc_token).digest('base64'); 15 reply.type('application/json').code(200); 16 return { response_token: 'sha256=' + digest }; 17}) 18 19fastify.post('/webhook', async (req, reply) => { 20 reply.type('text/plain').code(200); 21 return ''; 22}) 23 24fastify.listen(3000, (err, address) => { 25 if (err) throw err 26 fastify.log.info(`server listening on ${address}`) 27})
あなたの回答
tips
プレビュー