生成认证TokenV2
更新时间:2024-04-26
获取认证Token
鉴权的主要目的是获取accessToken。accessToken是用户的访问令牌,承载了用户的身份、权限等信息。鉴权主要分为以下两步:
- 获取Access Key/Secret Key
- 获取accessToken
获取Access key/Secret key
当您购买智能外呼产品后,可以登录智能外呼,在智能外呼的「系统管理」-> 「API配置」中可以获取到Access Key和Secret Key,如果是新用户,可以点击创建Access key,即可获取到Access Key和Secret Key。
平台将会分配给您此应用的相关凭证,主要为Access key、Secret Key。以上三个信息是您应用实际开发的重要凭证,每个应用各不相同,为了您的财产和服务安全请您妥善保管。
获取accessToken
接口描述
URL
https://aiob-open.baidu.com/api/v2/getToken
HTTP Method
POST
请求参数
Header
参数名 | 是否必需 | 说明 | 示例 |
---|---|---|---|
Content-Type | 是 | json标准的请求头 | application/json |
Param
参数名 | 是否必需 | 说明 | 示例 |
---|---|---|---|
accessKey | 是 | 智能外呼的Access Key | xxxx |
secretKey | 是 | 智能外呼的Secret Key | xxxx |
响应参数
参数名 | 数据类型 | 说明 |
---|---|---|
code | int | 响应码200成功,其他均为错误,具体看状态描述 |
msg | String | 状态描述,如 「ok」,「accessKey,不能为空」 |
data | array | 返回具体的响应内容 |
data
参数名 | 数据类型 | 说明 |
---|---|---|
tenantId | long | 租户Id |
accessToken | String | 返回的accessToken |
expiresTime | int | accessToken失效时间默认是30天 |
accessKey | String | 智能外呼的Access Key |
secretKey | String | 智能外呼的Secret Key |
请求示例
{
"secretKey":"xxx",
"accessKey":"xxx"
}
响应示例
{
"time": 1704194573489,
"code": 200,
"msg": "OK",
"data": {
"tenantId": 123123,
"accountId": "xxx",
"accessToken": "xxxx",
"expiresTime": 43200,
"accessKey": "xxx",
"secretKey": "xxx"
}
}
代码示例
Java代码示例
package baidu.com;
import okhttp3.*;
import org.json.JSONObject;
import java.io.IOException;
class Demo {
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
public static void main(String[] args) throws IOException {
MediaType mediaType = MediaType.parse("application/json");
JSONObject requestBodyJson = new JSONObject();
requestBodyJson.put("secretKey", "xxxx");
requestBodyJson.put("accessKey", "xxxx");
RequestBody body = RequestBody.create(mediaType, requestBodyJson.toString());
Request request = new Request.Builder()
.url("https://aiob-open.baidu.com/api/v2/getToken")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
System.out.println(response.body().string());
}
}
Go示例
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func main() {
url := "https://aiob-open.baidu.com/api/v2/getToken"
payload := strings.NewReader(`{
"secretKey": "xxxx",
"accessKey": "xxxx"
}`)
req, err := http.NewRequest("POST", url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
Python示例
import requests
import json
def main():
url = 'https://aiob-open.baidu.com/api/v2/getToken'
headers = {
'Content-Type': 'application/json'
}
data = {
"secretKey": "xxxx",
"accessKey": "xxxx"
}
response = requests.post(url, headers=headers, json=data)
print(response.text)
if __name__ == '__main__':
main()
PHP示例
class Demo {
public function run() {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://aiob-open.baidu.com/api/v2/getToken",
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept: application/json'
),
CURLOPT_POSTFIELDS => json_encode(array(
"secretKey" => "xxxx",
"accessKey" => "xxxx"
))
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}
$rtn = (new Sample())->run();
print_r($rtn);
C++示例
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <curl/curl.h>
#include <json/json.h>
#include <fstream>
inline size_t onWriteData(void *buffer, size_t size, size_t nmemb, void *userp)
{
std::string *str = static_cast<std::string *>(userp);
str->append(static_cast<char *>(buffer), size * nmemb);
return size * nmemb;
}
int main(int argc, char *argv[])
{
std::string result;
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://aiob-open.baidu.com/api/v2/getToken");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Accept: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "{\"secretKey\":\"xxxx\",\"accessKey\":\"xxxx\"}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onWriteData);
res = curl_easy_perform(curl);
std::cout << result;
}
curl_easy_cleanup(curl);
curl_global_cleanup();
return (int)res;
}
C#示例
using System;
using RestSharp;
namespace DemoApplication {
public class Demo {
public static void Main(string[] args) {
var client = new RestClient("https://aiob-open.baidu.com/api/v2/getToken");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
var body = @"{
""secretKey"": ""xxxx"",
""accessKey"": ""xxxx""
}";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
}
}
Node.js示例
const axios = require('axios');
async function main() {
try {
const response = await axios.post('https://aiob-open.baidu.com/api/v2/getToken', {
secretKey: 'xxxx',
accessKey: 'xxxx'
}, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
console.log(response.data);
} catch (error) {
console.error(error);
}
}
main();
Postman请求示例
密钥安全提示与止损方法
- 注意请勿将您的AK/SK以及生成的Access token与他人共享或硬编码到APP及终端,为保护您的资源安全,平台可能会针对恶意滥用token进行禁用
- 当前使用https获取accessToken,如发生凭证(即AK/SK或Access token)泄露,请及时在「系统管理」-> 「API配置」中删除Access Key和Secret Key