【问题标题】:Oauth2 access token returns "invalid client" errorOauth2 访问令牌返回“无效客户端”错误
【发布时间】:2021-08-12 14:46:37
【问题描述】:

我在获取访问令牌时遇到问题。获取身份验证代码后,当我调用 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;

}

【问题讨论】:

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


    【解决方案1】:

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

    您应该使用 application/x-www-form-urlencoded 主体发出 POST 请求,并且您应该包含 Authorization 标头以及在基本方案中编码的 client_secret。有关详细信息,请参阅OAuth2 RFC

    【讨论】:

    • 我试过你写的。它也不起作用。而且在 ZohoMail API 文档中也写到我应该以这种形式使用它: POST oauth/v2/token HOST:: accounts.zoho.com Query String: accounts.zoho.com/oauth/v2/token ?code=1000.********* *******************f160 &grant_type=authorization_code &client_id=1000.R2Z0W*********************Q5EN &client_secret=39c****************************************921b &redirect_uri=zylkerapps.com/oauth2callback &scope=VirtualOffice.folders.READ
    【解决方案2】:

    您需要根据您的数据中心更改accounts.zoho.com。例如,针对印度的 accounts.zoho.in。您可以在 Zoho Web 应用程序的 URL 中看到这一点。它写在他们的文档link

    【讨论】:

      猜你喜欢
      • 2013-11-02
      • 2017-08-28
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 2019-03-13
      • 2017-10-27
      • 1970-01-01
      • 2013-03-23
      相关资源
      最近更新 更多