代码之家  ›  专栏  ›  技术社区  ›  NextNightFlyer

我的Cloudflare Worker无法通过Twitter的开发者API向Twitter发布基本推文。我得到一个500内部服务器错误

  •  0
  • NextNightFlyer  · 技术社区  · 4 年前

    我正试图让一个cloudflare争吵者工作人员通过推特的开发者API向推特发布一条基本的推文。推特帖子应该是由一个简单的空白GET请求触发的。我有一个打字稿争吵工人模板和我的 src 目录如下所示:

    twitterClient.ts

    const { TwitterApi } = require('twitter-api-v2')
    
    const client = new TwitterApi ({
      appKey: "<APP KEY HERE>",
      appSecret: "<APP SECRET HERE>",
      accessToken: "<ACCESS TOKEN HERE>",
      accessSecret: "<ACCESS SECRET HERE>",
    });
    
    export const rwClient = client.readWrite
    
    

    手柄.ts

    export async function handleRequest(request: Request): Promise<Response> {
      return new Response(`Hello worker! this is a ${request.method} request`, {
        headers: { 'content-type': 'text/plain' },
      });
    }
    
    

    index.ts

    import { handleRequest } from './handler'
    import { rwClient } from './twitterClient'
    
    const tweet = async () => {
      await rwClient.v1.tweet("Tweeting from remote again")
      return new Response('the tweet function ran')
    }
    
    addEventListener('fetch', async (event) => {
      event.respondWith(tweet())
    })
    
    

    我通过打字来管理牧马工人 wrangler dev 在我的终端中,当牧马人工作人员正在运行时,我只是向 http://127.0.0.1:8787 与Postman,这样它将触发争吵工人发布推特。相反,当我运行GET请求时,我在终端中收到以下错误:

    <myusername>@<mycomputername> <myappname> % wrangler dev
    👂  Listening on http://127.0.0.1:8787
    🌀  Detected changes...
    💁  Ignoring stale first change
    [2022-04-23 05:24:06] GET <myappname>.<mycloudflareprojectname>.workers.dev/ HTTP/1.1 500 Internal Server Error
    TypeError: Cannot read properties of undefined (reading 'protocol')
        at Object.../../../Library/Caches/.wrangler/wranglerjs-1.19.11/node_modules/stream-http/index.js.http.request (worker.js:59047:40)
        at Object.../../../Library/Caches/.wrangler/wranglerjs-1.19.11/node_modules/https-browserify/index.js.https.request (worker.js:39342:23)
        at RequestHandlerHelper.buildRequest (worker.js:62593:28)
        at RequestHandlerHelper.makeRequest (worker.js:62618:14)
        at ClientRequestMaker.send (worker.js:62734:14)
        at TwitterApiv1.post (worker.js:63296:47)
        at TwitterApiv1.tweet (worker.js:67117:21)
        at tweet (worker.js:68933:39)
        at worker.js:68937:23 at line 59046, col 38
    {
      "exceptionDetails": {
        "columnNumber": 38,
        "exception": {
          "className": "TypeError",
          "description": "TypeError: Cannot read properties of undefined (reading 'protocol')\n    at Object.../../../Library/Caches/.wrangler/wranglerjs-1.19.11/node_modules/stream-http/index.js.http.request (worker.js:59047:40)\n    at Object.../../../Library/Caches/.wrangler/wranglerjs-1.19.11/node_modules/https-browserify/index.js.https.request (worker.js:39342:23)\n    at RequestHandlerHelper.buildRequest (worker.js:62593:28)\n    at RequestHandlerHelper.makeRequest (worker.js:62618:14)\n    at ClientRequestMaker.send (worker.js:62734:14)\n    at TwitterApiv1.post (worker.js:63296:47)\n    at TwitterApiv1.tweet (worker.js:67117:21)\n    at tweet (worker.js:68933:39)\n    at worker.js:68937:23",
          "preview": null,
          "subtype": "error",
          "type": "object",
          "value": null
        },
        "lineNumber": 59046,
        "text": "Uncaught (in promise)",
        "url": "worker.js"
      },
      "timestamp": <timestamp of post attempt>
    }
    TypeError: Cannot read properties of undefined (reading 'protocol') at line 0, col -2
    {
      "exceptionDetails": {
        "columnNumber": -2,
        "exception": {
          "className": "TypeError",
          "description": "TypeError: Cannot read properties of undefined (reading 'protocol')",
          "preview": null,
          "subtype": "error",
          "type": "object",
          "value": null
        },
        "lineNumber": 0,
        "text": "Uncaught (in response)",
        "url": "undefined"
      },
      "timestamp": <timestamp of post attempt>
    }
    ^C
    <myusername>@<mycomputername> <myappname> % 
    

    我如何让这个牧马人工作人员成功地发布推特?Twitter开发者文档的教程有一个谷歌云功能,通过Twitter的开发者API使用python函数发布到Twitter( https://developer.twitter.com/en/docs/tutorials/how-to-create-a-twitter-bot-with-twitter-api-v2 )。如果这是可能的,那么用Cloudflare争吵者的工作人员和打字脚本来复制这一点应该不会那么难吧?

    0 回复  |  直到 4 年前
    推荐文章