title: "认证端点 #" post_status: publish comment_status: open taxonomy: category: - woocommerce-rest-api post_tag: - V3 - Includes - Source
认证端点
自 WooCommerce 2.4 起,我们引入了认证端点。任何应用程序均可使用此端点,允许用户生成 API 密钥。这使得与 WooCommerce API 的集成更为简便,因为用户只需访问一个 URL 并点击“接受”。在重定向回应用程序后,API 密钥将通过 POST 请求发送。
下图展示了其工作原理:

URL parameters
| Parameter | Type | Description |
|---|---|---|
app_name |
string | Your app name mandatory |
scope |
string | Level of access. Available: read, write and read_write mandatory |
user_id |
string | User ID in your app. For your internal reference, used when the user is redirected back to your app. NOT THE USER ID IN WOOCOMMERCE mandatory |
return_url |
string | URL the user will be redirected to after authentication mandatory |
callback_url |
string | URL that will receive the generated API key. Note: this URL should be over HTTPS mandatory |
创建认证端点 URL
您必须使用 /wc-auth/v1/authorize 端点,并将上述参数作为查询字符串传递。
如何构建认证 URL 的示例:
# Bash 示例
STORE_URL='http://example.com'
ENDPOINT='/wc-auth/v1/authorize'
PARAMS="app_name=My App Name&scope=read_write&user_id=123&return_url=http://app.com/return-page&callback_url=https://app.com/callback-endpoint"
QUERY_STRING="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$PARAMS")"
QUERY_STRING=$(echo $QUERY_STRING | sed -e "s/%20/\+/g" -e "s/%3D/\=/g" -e "s/%26/\&/g")
echo "$STORE_URL$ENDPOINT?$QUERY_STRING"
var querystring = require('querystring');
var store_url = 'http://example.com';
var endpoint = '/wc-auth/v1/authorize';
var params = {
app_name: 'My App Name',
scope: 'read_write',
user_id: 123,
return_url: 'http://app.com/return-page',
callback_url: 'https://app.com/callback-endpoint'
};
var query_string = querystring.stringify(params).replace(/%20/g, '+');
console.log(store_url + endpoint + '?' + query_string);
<?php
$store_url = 'http://example.com';
$endpoint = '/wc-auth/v1/authorize';
$params = [
'app_name' => 'My App Name',
'scope' => 'write',
'user_id' => 123,
'return_url' => 'http://app.com',
'callback_url' => 'https://app.com'
];
$query_string = http_build_query( $params );
echo $store_url . $endpoint . '?' . $query_string;
?>
from urllib.parse import urlencode
store_url = 'http://example.com'
endpoint = '/wc-auth/v1/authorize'
params = {
"app_name": "My App Name",
"scope": "read_write",
"user_id": 123,
"return_url": "http://app.com/return-page",
"callback_url": "https://app.com/callback-endpoint"
}
query_string = urlencode(params)
print("%s%s?%s" % (store_url, endpoint, query_string))
require "uri"
store_url = 'http://example.com'
endpoint = '/wc-auth/v1/authorize'
params = {
app_name: "My App Name",
scope: "read_write",
user_id: 123,
return_url: "http://app.com/return-page",
callback_url: "https://app.com/callback-endpoint"
}
query_string = URI.encode_www_form(params)
puts "#{store_url}#{endpoint}?#{query_string}"
随 API 密钥一起发布的 JSON 示例
{
"key_id": 1,
"user_id": 123,
"consumer_key": "ck_xxxxxxxxxxxxxxxx",
"consumer_secret": "cs_xxxxxxxxxxxxxxxx",
"key_permissions": "read_write"
}
用户将看到的界面示例:

注意事项与提示
- 使用
return_url重定向用户时,还会以查询字符串形式发送success和user_id参数。 success参数:用户拒绝时发送0,认证成功时发送1。- 当用户被重定向回 (
return_url) 时,使用user_id来识别用户,并记得在认证后向你的callback_url发送 POST 请求时保存 API 密钥。 - 认证端点将以 JSON 格式向
callback_url发送 API 密钥,请注意某些语言(如 PHP)不会在$_POST全局变量中显示它。在 PHP 中,可以使用$HTTP_RAW_POST_DATA(适用于旧版 PHP)或file_get_contents('php://input');来访问。 - 此认证端点仅用于简化与 WooCommerce REST API 的集成。切勿将其用作客户的登录端点!