跳转至

获取Access Token

最后更新:2022年07月12日

请求方法

HTTP方法: POST
请求URL:http://api.amsu-new.com/amsu-analysis/v2/token
数据提交方式:application/x-www-form-urlencoded

Body参数:

字段 类型 必传 范围 描述
client_id String - 即 app_key,请联系AMSU获取
client_secret String - 即 app_secret,请联系AMSU获取

返回参数

字段 类型 描述
access_token String access token值,用于在调用接口时鉴定权限
token_type String token类型,本接口返回固定值 bearer
expires_in Integer access token有效时间,单位为秒
refresh_token String 用于刷新access token,有效期30天,参见 刷新 Access Token

正确返回示例:

HTTP状态码:200

{
    "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhcHBfa2V5OjkzZWUyMGNiNTliOTY0OWY3YzIwIiwiZXhwIjoxNjQ4MTg3MjYxfQ.PQvfy6uavPEfB_gzF68aqtudd4qhzNPHJt098UrQftI",
    "token_type": "bearer",
    "expires_in": 86400,
    "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJyZWZyZXNoOmFwcF9rZXk6OTNlZTIwY2I1OWI5NjQ5ZjdjMjAiLCJleHAiOjE2NDk3NDk5MzZ9.ETjPSnch3LbqOy-qKmma5XlmYv6L-Dq5_OUu3tqieQE"
}
错误返回示例:

HTTP状态码不为200

{
    "error": {
        "code": "COMMON.AUTHENTICATION_FAILED",
        "msg": "Incorrect client id or secret."
    }
}

错误码

参见 公共错误码

示例代码

OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "client_id=5348724013ab2c09ade2&client_secret=24a0a4fd107c503f5d34");
Request request = new Request.Builder()
  .url("http://api.amsu-new.com/amsu-analysis/v2/token")
  .method("POST", body)
  .addHeader("Content-Type", "application/x-www-form-urlencoded")
  .build();
Response response = client.newCall(request).execute();
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.amsu-new.com/amsu-analysis/v2/token"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"client_id=5348724013ab2c09ade2" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&client_secret=24a0a4fd107c503f5d34" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");

var urlencoded = new URLSearchParams();
urlencoded.append("client_id", "5348724013ab2c09ade2");
urlencoded.append("client_secret", "24a0a4fd107c503f5d34");

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: urlencoded,
  redirect: 'follow'
};

fetch("http://api.amsu-new.com/amsu-analysis/v2/token", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
import requests

url = "http://api.amsu-new.com/amsu-analysis/v2/token"

payload='client_id=5348724013ab2c09ade2&client_secret=24a0a4fd107c503f5d34'
headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

刷新 Access Token

请求方法

HTTP方法: PUT
请求URL:http://api.amsu-new.com/amsu-analysis/v2/token
数据提交方式:application/x-www-form-urlencoded

Body参数:

字段 类型 必传 范围 描述
refresh_token String - -

接口 返回参数 同获取Access Token

提示

若由于客户端或防火墙限制无法使用PUT方法,请使用POST方法请求http://api.amsu-new.com/amsu-analysis/v2/token/refreshment获取刷新。

示例代码

OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "refresh_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJyZWZyZXNoOmFwcF9rZXk6OTNlZTIwY2I1OWI5NjQ5ZjdjMjAiLCJleHAiOjE2NDgxMDQ2MzN9.xCOLgbFJS0UI-evqJW95lxtHrZD4zITkXYfvJouOcgk");
Request request = new Request.Builder()
  .url("http://api.amsu-new.com/amsu-analysis/v2/token")
  .method("PUT", body)
  .addHeader("Content-Type", "application/x-www-form-urlencoded")
  .build();
Response response = client.newCall(request).execute();
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.amsu-new.com/amsu-analysis/v2/token"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"refresh_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJyZWZyZXNoOmFwcF9rZXk6OTNlZTIwY2I1OWI5NjQ5ZjdjMjAiLCJleHAiOjE2NDgxMDQ2MzN9.xCOLgbFJS0UI-evqJW95lxtHrZD4zITkXYfvJouOcgk" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];

[request setHTTPMethod:@"PUT"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");

var urlencoded = new URLSearchParams();
urlencoded.append("refresh_token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJyZWZyZXNoOmFwcF9rZXk6OTNlZTIwY2I1OWI5NjQ5ZjdjMjAiLCJleHAiOjE2NDgxMDQ2MzN9.xCOLgbFJS0UI-evqJW95lxtHrZD4zITkXYfvJouOcgk");

var requestOptions = {
  method: 'PUT',
  headers: myHeaders,
  body: urlencoded,
  redirect: 'follow'
};

fetch("http://api.amsu-new.com/amsu-analysis/v2/token", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
import requests

url = "http://api.amsu-new.com/amsu-analysis/v2/token"

payload='refresh_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJyZWZyZXNoOmFwcF9rZXk6OTNlZTIwY2I1OWI5NjQ5ZjdjMjAiLCJleHAiOjE2NDgxMDQ2MzN9.xCOLgbFJS0UI-evqJW95lxtHrZD4zITkXYfvJouOcgk'
headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.request("PUT", url, headers=headers, data=payload)

print(response.text)