【问题标题】:Missing required field "grant_type" in dropbox APIDropbox API 中缺少必填字段“grant_type”
【发布时间】:2017-10-26 08:51:38
【问题描述】:

我正在尝试使用 dropbox api 显示主列表文件夹,当我尝试从授权用户获取访问令牌时遇到很多问题。

我的代码很简单,我通过授予我的应用程序权限来获取代码。而获取token的请求是这样的。

$client = new GuzzleHttp\Client();

try{
//    $response = $client->get("https://www.dropbox.com/oauth2/authorize?client_id=". $client_id . "&response_type=code");

    $response = $client->post("https://api.dropboxapi.com/oauth2/token",
        array(
            'json'  => array(
                'grant_type'    => 'authorization_code',
                'code' => '********ZJtDI'
            ),
            'auth'      => array(
                $client_id,
                $client_secret
            ), 
        ));

    echo $response->getBody();

}catch ( \Exception $e ){
    echo $e->getMessage();
}

Client error: `POST https://api.dropboxapi.com/oauth2/token` resulted in a `400 Bad Request` response:
{"error_description": "missing required field \"grant_type\"", "error": "invalid_request"}

【问题讨论】:

    标签: php dropbox-api guzzle


    【解决方案1】:

    您的 POST 请求必须包含 application/x-www-form-urlencoded 编码的 POST 数据,而不是 JSON 字符串。见https://www.dropbox.com/developers-v1/core/docs#oa2-token。并阅读 http://docs.guzzlephp.org/en/stable/quickstart.html#post-form-requests 发送带有普通表单字段的 POST 请求。

    【讨论】:

      【解决方案2】:

      正如 Progman 所说,您必须使用通常的表单类型 (application/x-www-form-urlencoded) 而不是 JSON。

      使用 Guzzle 很简单:

      $response = $client->post("https://api.dropboxapi.com/oauth2/token",
          array(
              'form_params' => array(
                  'grant_type' => 'authorization_code',
                  'code' => '********ZJtDI',
                  'client_id' => $client_id,
                  'client_secret' => $client_secret,
              ),
          )
      );
      

      【讨论】:

        猜你喜欢
        • 2012-06-27
        • 2016-06-09
        • 1970-01-01
        • 2021-10-03
        • 2018-07-19
        • 2014-08-02
        • 1970-01-01
        • 1970-01-01
        • 2019-09-14
        相关资源
        最近更新 更多