鉴权
- 客户端通过OAuth 2.0 Client Credentials方式进行鉴权
- 请提前获取您的 API Key 和 API Secret
- 鉴权URL:https://login.chinacloudapi.cn/387c56a8-cf86-45de-b9f5-b4de88cac054/oauth2/v2.0/token,以下统称为 AUTH_URL
获取Access Token¶
请求:
- 请求URL: AUTH_URL
- http方法: POST
- header : "Content-Type": "application/x-www-form-urlencoded"
- body:
- "grant_type":"client_credentials"
- "client_id":您的API Key
- "client_secret": 您的API Secret
- "Type":"oauth2"
- "scope": d6b7c858-55fa-4d04-85a6-131af690abd4/.default
响应:
{
"token_type": "Bearer", /*token 类型*/
"expires_in": 3599, /*有效期/秒*/
"ext_expires_in": 3599,
"access_token": ".........." /*授权码*/
}
如果Token过期,请重新使用该接口获取token
此步骤中获取的 access_token 作为访问API的凭证,在有效期内都可用此access_token,推荐客户端在有效期内缓存该 access_token
示例代码
curl -X "POST" "https://login.chinacloudapi.cn/387c56a8-cf86-45de-b9f5-b4de88cac054/oauth2/v2.0/token" \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_id=替换API Key" \
--data-urlencode "client_secret=替换API Secret" \
--data-urlencode "Type=oauth2" \
--data-urlencode "scope=d6b7c858-55fa-4d04-85a6-131af690abd4/.default"
private const string AppClientId = "您的API Key";
private const string AppClientSecret = "您的API Secret";
private const string BaseUri = https://login.chinacloudapi.cn/387c56a8-cf86-45de-b9f5-b4de88cac054/oauth2/v2.0/token;
static void Main(string[] args)
{
var tokenString = GetDevCenterApiToken();
Console.WriteLine("BearerToken:" + tokenString);
}
static string GetDevCenterApiToken()
{
var tokenString = string.Empty;
var httpclientHandler = new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true,
};
var fromContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string,string>("client_id", AppClientId),
new KeyValuePair<string,string>("client_secret", AppClientSecret),
new KeyValuePair<string, string>("scope", "d6b7c858-55fa-4d04-85a6-131af690abd4/.default"),
new KeyValuePair<string,string>("grant_type", "client_credentials"),
});
using (var httpClient = new HttpClient(httpclientHandler))
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var result = httpClient.PostAsync(BaseUri, fromContent).Result;
tokenString = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(result.Content.ReadAsStringAsync().Result);
httpClient.Dispose();
}
return tokenString;
}
import requests
def send_request():
# 获取token
# POST https://login.chinacloudapi.cn/41ebb163-adc1-4068-9087-eb79c718b633/oauth2/v2.0/token
try:
response = requests.post(
url="https://login.chinacloudapi.cn/387c56a8-cf86-45de-b9f5-b4de88cac054/oauth2/v2.0/token",
headers={
"Content-Type": "application/x-www-form-urlencoded",
},
data={
"grant_type": "client_credentials",
"client_id": "替换API Key",
"client_secret": "API Secret",
"Type": "oauth2",
"scope": "d6b7c858-55fa-4d04-85a6-131af690abd4/.default",
},
)
print('Response HTTP Status Code: {status_code}'.format(
status_code=response.status_code))
print('Response HTTP Response Body: {content}'.format(
content=response.content))
except requests.exceptions.RequestException:
print('HTTP Request failed')
通过以上操作,您将获取您的Access Token,后续请求接口时,需要将其放到Headers中,格式为:Authorization: Bearer {access token}