使用 curl 命令堆栈交换 API

Stack Exchange API with curl command

通过关注此 Stack Exchange API documentation, I have registered my application on Stack Apps (here)。注册后,我通过身份验证创建了 request keyaccess_tokens

我需要通过 curl 命令 运行 Stack Exchange API,为此,我 运行 以下命令

curl "https://api.stackexchange.com/2.2/answers/43919322?order=desc&sort=activity&site=Whosebug&access_token=my-access-token)EuzzA((&key=5RseNTJmS0C35DTMsc2dYQ((" | gunzip

点击上述请求后,出现以下错误:

{"error_id":403,"error_message":"`key` is not valid for passed `access_token`, token not found.","error_name":"access_denied"}

对于身份验证,我参考了 this 并使用查询字符串参数点击 URL https://whosebug.com/oauth/dialog,但这里也没有显示任何响应。

任何人都可以告诉我为什么会出现此错误,即使 curl 请求中提到的参数是正确的,或者是否还有任何验证步骤?

根据上面的讨论和@Tanaike 给出的解决方案,我能够解决问题。

为了使用curl命令运行 Stack Exchange API,我们需要遵循以下步骤。要获得详细说明,请参阅 Stack Exchange API.

的官方文档

身份验证步骤

  1. 应用程序应在 Stack Apps to get a request key. Request keys grant more requests per day and are necessary for using access_tokens created via authentication.

    上注册
  2. 要注册您的应用程序(单击 Stack Apps),写入应用程序名称、描述并在 OAuth 域中写入 stackexchange.com.

    请参阅 this answer,以获取有关注册应用程序的详细信息。

  3. 注册应用程序后,将生成客户端 ID、客户端密码和 API 密钥。

  4. 现在从浏览器中点击 URL https://whosebug.com/oauth/dialog client_id=###&scope=no_expiry&redirect_uri=http://stackexchange.com。这将重定向到 http://stackexchange.com/#access_token=###, where ###` 是访问令牌。

  5. 现在运行这个带有认证参数的curl命令

    curl "https://api.stackexchange.com/2.2/answers/43919322?order=desc&sort=activity&site=Whosebug&access_token=my-access-token&key=my-key" | gunzip
    
  6. 运行执行上述 curl 命令后,将生成响应。