生成认证TokenV2
更新时间:2025-03-17
获取认证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 | object | 返回具体的响应内容见示例 |
data
参数名 | 数据类型 | 说明 |
---|---|---|
tenantId | long | 租户Id |
accessToken | String | 返回的accessToken |
expiresTime | int | accessToken失效时间默认是30天 |
accessKey | String | 智能外呼的Access Key |
secretKey | String | 智能外呼的Secret Key |
请求示例
Plain Text
1{
2 "secretKey":"xxx",
3 "accessKey":"xxx"
4}
响应示例
Plain Text
1{
2 "time": 1704194573489,
3 "code": 200,
4 "msg": "OK",
5 "data": {
6 "tenantId": 123123,
7 "accountId": "xxx",
8 "accessToken": "xxxx",
9 "expiresTime": 43200, // 过期时间 单位 分钟
10 "accessKey": "xxx",
11 "secretKey": "xxx"
12 }
13}
代码示例
Java代码示例
Plain Text
1package baidu.com;
2
3import okhttp3.*;
4import org.json.JSONObject;
5
6import java.io.IOException;
7
8class Demo {
9
10 static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
11
12 public static void main(String[] args) throws IOException {
13 MediaType mediaType = MediaType.parse("application/json");
14 JSONObject requestBodyJson = new JSONObject();
15 requestBodyJson.put("secretKey", "xxxx");
16 requestBodyJson.put("accessKey", "xxxx");
17 RequestBody body = RequestBody.create(mediaType, requestBodyJson.toString());
18 Request request = new Request.Builder()
19 .url("https://aiob-open.baidu.com/api/v2/getToken")
20 .method("POST", body)
21 .addHeader("Content-Type", "application/json")
22 .build();
23 Response response = HTTP_CLIENT.newCall(request).execute();
24 System.out.println(response.body().string());
25 }
26}
Go示例
Plain Text
1package main
2
3import (
4 "fmt"
5 "io/ioutil"
6 "net/http"
7 "strings"
8)
9
10func main() {
11 url := "https://aiob-open.baidu.com/api/v2/getToken"
12 payload := strings.NewReader(`{
13 "secretKey": "xxxx",
14 "accessKey": "xxxx"
15 }`)
16 req, err := http.NewRequest("POST", url, payload)
17 if err != nil {
18 fmt.Println(err)
19 return
20 }
21 req.Header.Add("Content-Type", "application/json")
22
23 client := &http.Client{}
24 res, err := client.Do(req)
25 if err != nil {
26 fmt.Println(err)
27 return
28 }
29 defer res.Body.Close()
30
31 body, err := ioutil.ReadAll(res.Body)
32 if err != nil {
33 fmt.Println(err)
34 return
35 }
36 fmt.Println(string(body))
37}
Python示例
Plain Text
1import requests
2import json
3
4def main():
5 url = 'https://aiob-open.baidu.com/api/v2/getToken'
6 headers = {
7 'Content-Type': 'application/json'
8 }
9 data = {
10 "secretKey": "xxxx",
11 "accessKey": "xxxx"
12 }
13
14 response = requests.post(url, headers=headers, json=data)
15 print(response.text)
16
17if __name__ == '__main__':
18 main()
PHP示例
Plain Text
1class Demo {
2
3 public function run() {
4 $curl = curl_init();
5 curl_setopt_array($curl, array(
6 CURLOPT_URL => "https://aiob-open.baidu.com/api/v2/getToken",
7 CURLOPT_TIMEOUT => 30,
8 CURLOPT_RETURNTRANSFER => true,
9 CURLOPT_CUSTOMREQUEST => 'POST',
10 CURLOPT_HTTPHEADER => array(
11 'Content-Type: application/json',
12 'Accept: application/json'
13 ),
14 CURLOPT_POSTFIELDS => json_encode(array(
15 "secretKey" => "xxxx",
16 "accessKey" => "xxxx"
17 ))
18 ));
19 $response = curl_exec($curl);
20 curl_close($curl);
21 return $response;
22 }
23}
24$rtn = (new Sample())->run();
25print_r($rtn);
C++示例
Plain Text
1#include <stdio.h>
2#include <iostream>
3#include <string.h>
4#include <curl/curl.h>
5#include <json/json.h>
6#include <fstream>
7
8
9inline size_t onWriteData(void *buffer, size_t size, size_t nmemb, void *userp)
10{
11 std::string *str = static_cast<std::string *>(userp);
12 str->append(static_cast<char *>(buffer), size * nmemb);
13 return size * nmemb;
14}
15
16
17int main(int argc, char *argv[])
18{
19 std::string result;
20 CURL *curl;
21 CURLcode res;
22 curl_global_init(CURL_GLOBAL_DEFAULT);
23 curl = curl_easy_init();
24 if (curl) {
25 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
26 curl_easy_setopt(curl, CURLOPT_URL, "https://aiob-open.baidu.com/api/v2/getToken");
27 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
28 curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http");
29 struct curl_slist *headers = NULL;
30 headers = curl_slist_append(headers, "Content-Type: application/json");
31 headers = curl_slist_append(headers, "Accept: application/json");
32 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
33 const char *data = "{\"secretKey\":\"xxxx\",\"accessKey\":\"xxxx\"}";
34 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
35 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
36 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onWriteData);
37 res = curl_easy_perform(curl);
38 std::cout << result;
39 }
40 curl_easy_cleanup(curl);
41 curl_global_cleanup();
42 return (int)res;
43}
C#示例
Plain Text
1using System;
2using RestSharp;
3
4namespace DemoApplication {
5 public class Demo {
6 public static void Main(string[] args) {
7 var client = new RestClient("https://aiob-open.baidu.com/api/v2/getToken");
8 client.Timeout = -1;
9 var request = new RestRequest(Method.POST);
10 request.AddHeader("Content-Type", "application/json");
11 request.AddHeader("Accept", "application/json");
12 var body = @"{
13 ""secretKey"": ""xxxx"",
14 ""accessKey"": ""xxxx""
15 }";
16 request.AddParameter("application/json", body, ParameterType.RequestBody);
17 IRestResponse response = client.Execute(request);
18 Console.WriteLine(response.Content);
19 }
20 }
21}
Node.js示例
Plain Text
1const axios = require('axios');
2
3async function main() {
4 try {
5 const response = await axios.post('https://aiob-open.baidu.com/api/v2/getToken', {
6 secretKey: 'xxxx',
7 accessKey: 'xxxx'
8 }, {
9 headers: {
10 'Content-Type': 'application/json',
11 'Accept': 'application/json'
12 }
13 });
14
15 console.log(response.data);
16 } catch (error) {
17 console.error(error);
18 }
19}
20
21main();
Postman请求示例
密钥安全提示与止损方法
- 注意请勿将您的AK/SK以及生成的Access token与他人共享或硬编码到APP及终端,为保护您的资源安全,平台可能会针对恶意滥用token进行禁用
- 当前使用https获取accessToken,如发生凭证(即AK/SK或Access token)泄露,请及时在「系统管理」-> 「API配置」中删除Access Key和Secret Key