當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP WHMCS CreateOAuthCredential用法及代碼示例

創建 OAuth 憑據

請求參數

參數 類型 說明 必需的
action string “CreateOAuthCredential” Required
grantType string ‘authorization_code’ 或 ‘single_sign_on’ 之一 Required
scope string tbloauthserver_scopes 中有效範圍的空格分隔列表 Required
name string 為 authorization_code $grantType 提供 oAuth 憑據的名稱 Optional
serviceId int single_sign_on $grantType 的服務 ID Optional
description string OAuth 憑證的說明 Optional
logoUri string 此應用程序的徽標圖像文件的相對於基本 WHMCS 客戶區目錄的 URL 或路徑。 Optional
redirectUri string 授權的重定向 URI Optional

響應參數

參數 類型 說明
result string 操作結果:成功或錯誤
credentialId int 新創建的oauth憑證的ID
clientIdentifier string 隨機生成的 oauth 客戶端標識符
clientSecret string 身份驗證憑據的隨機生成的秘密

示例請求 (CURL)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
    http_build_query(
        array(
            'action' => 'CreateOAuthCredential',
            // See https://developers.whmcs.com/api/authentication
            'username' => 'IDENTIFIER_OR_ADMIN_USERNAME',
            'password' => 'SECRET_OR_HASHED_PASSWORD',
            'grantType' => 'single_sign_on',
            'scope' => 'clientarea:sso clientarea:billing_info clientarea:announcements',
            'serviceId' => '1',
            'description' => 'Billing and Announcements SSO',
            'responsetype' => 'json',
        )
    )
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

示例請求(本地 API)

$command = 'CreateOAuthCredential';
$postData = array(
    'grantType' => 'single_sign_on',
    'scope' => 'clientarea:sso clientarea:billing_info clientarea:announcements',
    'serviceId' => '1',
    'description' => 'Billing and Announcements SSO',
);
$adminUsername = 'ADMIN_USERNAME'; // Optional for WHMCS 7.2 and later

$results = localAPI($command, $postData, $adminUsername);
print_r($results);

示例響應 JSON

{
    "result": "success",
    "credentialId": "1",
    "clientIdentifier": "COMPANY-NAME.SQxOYrZOUTQC8YTkLQuQ0w==",
    "clientSecret": "60XScB\/W8zzxtciWlvX8+OoO4ZAQj8dNLeFIulRlYhDgksrSJd0Olv+X7wyoJEWEOpZ9IivCaySN7s+\/a++Tlg=="
}

錯誤響應

可能的錯誤條件響應包括:

  • 需要有效的授權類型。
  • 請求的授權類型“x” 無效。
  • 需要客戶端憑據的名稱。
  • 單個 sign-on 授權類型需要服務 ID。
  • 至少需要一個有效範圍。
  • 請求的範圍 “x” 無效。
  • 需要有效的服務 ID。
  • 服務 ID 未與有效客戶端關聯。

相關用法


注:本文由純淨天空篩選整理自whmcs.com大神的英文原創作品 CreateOAuthCredential。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。