You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP调用Zoom API获取OAuth令牌遇unsupported_grant_type错误求助

问题:Zoom API获取OAuth令牌报错"unsupported_grant_type"

尝试通过PHP调用Zoom API实现向Webinar添加注册者的功能,流程为先获取OAuth令牌,再发起添加请求,但获取令牌时返回错误:

OAuth Request Failed: 400 {"reason":"unsupported grant type","error":"unsupported_grant_type"}

相关代码

class Zoom_Api
{
    public function getToken() {
        $url = 'https://zoom.us/oauth/token';
        $data = array(
            'grant_type' => 'account_credentials',
            'account_id' => '*******'
        );
        
        $zoomAPIKey  = '************';
        $zoomSecretKey  = '************';

        $headers = array(
            'Authorization: Basic '. base64_encode($zoomAPIKey.":".$zoomSecretKey)
        );
        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);

        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if ($httpCode == 200) {
            $oauthResponse = json_decode($response, true);
            $accessToken = $oauthResponse['access_token'];
            return $accessToken;
        } else {
            echo 'OAuth Request Failed: ' . $httpCode . PHP_EOL;
            echo $response . PHP_EOL;
            return null;
        }
        
        curl_close($curl);
    }

    public function sentRequest() {
        $name = "testdfdf";
        $email = "sdfd@dfdfdfdf.com";
        $webinarID = 867098960000;

        $regData = array(
            'email' => $email,
            'first_name' => $name,
        );

        $jsonStrReg = json_encode($regData);
        $httpStrReg = http_build_query($regData);
        
        $ch = curl_init('https://api.zoom.us/v2/webinars/'.$webinarID.'/registrants');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $httpStrReg);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Bearer ' . $this->getToken()
        ));

        $response = curl_exec($ch);
        $response = json_decode($response);
        curl_close($ch);
        return $response;
    }
}

$zoom_meeting = new Zoom_Api();
$response = $zoom_meeting->getToken();
echo "<pre>";
print_r($response);
echo "</pre>";

错误原因及修复方案

核心原因

Zoom的S2S OAuth接口要求请求必须明确指定Content-Type: application/x-www-form-urlencoded,否则无法正确解析grant_type参数,导致返回"unsupported_grant_type"错误。此外代码中存在其他潜在问题也需修正。

具体修复步骤

  1. 添加Content-Type头部
    在getToken方法的headers数组中添加Content-Type声明:

    $headers = array(
        'Authorization: Basic '. base64_encode($zoomAPIKey.":".$zoomSecretKey),
        'Content-Type: application/x-www-form-urlencoded'
    );
    
  2. 调整curl_close位置
    将curl_close($curl);移到if-else语句之前,避免httpCode非200时curl资源泄漏:

    curl_close($curl);
    
    if ($httpCode == 200) {
        // ... 原有逻辑
    } else {
        // ... 原有逻辑
    }
    
  3. 验证账号与应用信息

    • 确认account_id是Zoom主账号的ID(非子账号ID)
    • 确保zoomAPIKey和zoomSecretKey属于S2S OAuth类型的应用(不是JWT或OAuth授权码类型)
  4. 修复添加注册者的请求格式
    添加Webinar注册者的API要求请求体为JSON格式,需修改sentRequest方法:

    public function sentRequest() {
        $name = "testdfdf";
        $email = "sdfd@dfdfdfdf.com";
        $webinarID = 867098960000;
    
        $regData = array(
            'email' => $email,
            'first_name' => $name,
        );
    
        $jsonStrReg = json_encode($regData);
        
        $ch = curl_init('https://api.zoom.us/v2/webinars/'.$webinarID.'/registrants');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStrReg);
    
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Bearer ' . $this->getToken(),
            'Content-Type: application/json'
        ));
    
        $response = curl_exec($ch);
        $response = json_decode($response);
        curl_close($ch);
        return $response;
    }
    

内容的提问来源于stack exchange,提问作者Dor Ben Zaken

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 07:07:55