Twitter帐户活动API-如何为事件订阅Webhooks?

时间:2020-07-12 14:55:09

标签: api go twitter webhooks subscription

我正在使用golang开发一个Twitter机器人。我一直按照here

上的说明进行操作

为了使我的Webhook监听(订阅)Twitter帐户(机器人)的活动,我必须为我的Webhook订阅事件。该准则在here

这就是我被困住的地方。我已经成功注册了Webhook,但仍然无法为其订阅事件。

这是我运行的代码:

func main() {
    //Load env
    err := godotenv.Load()
    if err != nil {
        log.Fatal("Error loading .env file")
        log.Println("Error loading .env file")
    }


    subscribeWebhook()
}

func CreateClient() *http.Client {
    //Create oauth client with consumer keys and access token
    config := oauth1.NewConfig(os.Getenv("CONSUMER_KEY"), os.Getenv("CONSUMER_SECRET"))
    token := oauth1.NewToken(os.Getenv("ACCESS_TOKEN_KEY"), os.Getenv("ACCESS_TOKEN_SECRET"))

    return config.Client(oauth1.NoContext, token)
}

func subscribeWebhook() {
    log.Println("Subscribing webapp...")
    client := CreateClient()
    path := "https://api.twitter.com/1.1/account_activity/all/" + os.Getenv("WEBHOOK_ENV") + "/subscriptions.json"
    resp, err := client.PostForm(path, nil)
    if err != nil {
        panic(err)
    }
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    //If response code is 204 it was successful
    if resp.StatusCode == 204 {
        log.Println("Subscribed successfully")
    } else if resp.StatusCode != 204 {
        log.Println("Could not subscribe the webhook. Response below:")
        log.Println(string(body))
    }
}

这是我得到的答复:

{
    "errors": [
        {
            "code": 348,
            "message": "Client application is not permitted to access this user's webhook subscriptions."
        }
    ]
}

请,任何人都可以帮助我解决这个问题。谢谢大家。

1 个答案:

答案 0 :(得分:1)

我知道了。

结果是,必须在开发人员门户中的应用程序设置上为您的应用程序授予完全权限(读取,写入和直接消息)。然后,重新生成您的密钥和令牌。最后,使用新的键和令牌更新环境变量。