Oauth2访问令牌返回“无效的客户端”错误

时间:2019-05-20 06:46:43

标签: api curl oauth-2.0 access-token zoho

我在获取访问令牌时遇到问题。获取身份验证代码后,当我调用我的get_access_token时,它将返回“ invalid_client”错误。我对此进行了研究,但没有任何帮助。请查看我的代码,并帮助我解决此问题。先感谢您。 这是我的代码:

public function get_access_token($zoho_code)
{
    $headers = array(

    );
    $taskurl = 'https://accounts.zoho.com/oauth/v2/token';
    $cdata = array(
        'code' => $zoho_code,
        'grant_type' => 'authorization_code',
        'client_id' =>  $this->client_id,
        'client_secret' => $this->client_secret_id,
        'redirect_uri' => 'http://localhost/callback.php',
        'scope' => 'ZohoMail.accounts.UPDATE,ZohoMail.accounts.READ,ZohoMail.partner.organization.READ,ZohoMail.partner.organization.UPDATE,ZohoMail.organization.accounts.CREATE,ZohoMail.organization.accounts.UPDATE,ZohoMail.organization.accounts.READ,ZohoMail.organization.domains.CREATE,ZohoMail.organization.domains.UPDATE,ZohoMail.organization.domains.DELETE,ZohoMail.organization.domains.READ',
        'state' => '55555sfdfsdfgbcv',

    );
    $curlresult = $this->docurl($taskurl, $cdata, $headers);

    return $curlresult;
}

public function docurl($taskurl, $cdata, $headers, $method = 'post',$sendjson=true) {

    $ch = curl_init();

    if ($method == 'get') {
        if ($cdata) {
            $query = '?' . http_build_query($cdata);
            $taskurl .= $query;
        }
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    } elseif ($method == 'delete') {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    } elseif ($method == 'put') {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    } elseif ($method == 'patch') {
        if($sendjson) $cdata = json_encode($cdata);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $cdata);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
    } else {

        if($sendjson) $cdata = json_encode($cdata);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $cdata);
    }
    curl_setopt($ch, CURLOPT_URL, $taskurl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $res = curl_exec($ch);
    $information = curl_getinfo($ch);

    print_r($information);
    print_r($cdata);

    curl_close($ch);
    $resj = json_decode($res);
     return $resj;

}

1 个答案:

答案 0 :(得分:0)

如果我正确阅读了您的代码,则说明您将client_secret作为JSON编码的POST请求正文的一部分发送。

您应使用application/x-www-form-urlencoded正文进行POST请求,并应将Authorization标头包括在基本方案中编码的client_secret中。有关更多信息,请参见OAuth2 RFC