tokens
The options parameter in all methods described in this topic specifies additional request configurations. For more information, see AxiosRequestConfig.
How do I get a token?
Tokens can be obtained in multiple ways depending on the application type. For more information, see Application overview.
All methods in this topic return tokens. Pass the auth_endpoint parameter to the constructor:
const client = new PDSClient({
auth_endpoint: `https://${domain_id}.api.aliyunpds.com`
})
Token lifecycle
All token types share the same expiration rules:
access_token: valid for 2 hoursrefresh_token: valid for 7 days
Call refreshJwtToken or refreshToken at any point before the refresh_token expires to get a new token, regardless of whether the access_token is still valid. Refreshing invalidates the old token and returns a new one.
When to use each method
|
Method |
Use when |
|
|
Your app manages its own identity system and needs to act on behalf of a specific user without requiring the user to log in through PDS |
|
|
Your app needs super administrator permissions for Domain-level operations management |
|
|
Your app uses the OAuth 2.0 authorization code flow and needs to exchange the one-time code for a user token |
|
|
A JSON Web Token (JWT)-based |
|
|
An OAuth-based |
The getUserJwtToken method
Added in v0.1.2
Gets a user token via JSON Web Token (JWT) for quick PDS integration.
Note: This method applies only to Node.js.
Prerequisites
Before calling this method:
Create a JWT application in the PDS console.
Configure the public key in the application settings.
Download the private key.
Syntax
const result = await client.getUserJwtToken(params, options)
Example
const domain_id = 'Your Domain ID'
const client_id = 'Your JWT application ID'
const private_key_pem = 'Your private key'
const user_id = 'The ID of the user for whom to get the token'
const client = new PDSClient({
auth_endpoint: `https://${domain_id}.api.aliyunpds.com`
})
const params = {
domain_id,
client_id,
private_key_pem,
user_id,
auto_create: false, // Specifies whether to automatically create the user if the user does not exist.
}
const jwtToken = await client.getUserJwtToken(params)
Parameters
|
Field |
Type |
Required |
Description |
|
params |
IGetUserJwtTokenReq |
Yes |
Parameters |
|
options |
AxiosRequestConfig |
Additional optional configurations. |
IGetUserJwtTokenReq
|
Field |
Type |
Required |
Description |
|
client_id |
string |
Yes |
The JWT application ID. |
|
private_key_pem |
string |
Yes |
The private key PEM string. |
|
domain_id |
string |
Yes |
The Domain ID. |
|
user_id |
string |
Yes |
The unique identifier of the user. Must be unique within the Domain. |
|
auto_create |
boolean |
Specifies whether to automatically create the user if the user does not exist. If an initial disk is configured on the domain, an initial disk is also created for the user automatically. Default: false. |
Returns
|
Field |
Type |
Required |
Description |
|
result |
ITokenInfo |
Yes |
A token representing the user identity. |
ITokenInfo
|
Field |
Type |
Required |
Description |
|
access_token |
string |
Yes |
The JWT representing the user identity. Valid for 2 hours. |
|
refresh_token |
string |
The refresh token. Use it to refresh an expired |
|
|
expire_time |
string |
The expiration time of the |
|
|
expires_in |
string |
The time-to-live (TTL) of the |
|
|
token_type |
string |
The type of the |
|
|
user_id |
string |
The unique identifier of the current user. |
|
|
role |
string |
The role of the current user. |
|
|
...Other fields |
For other fields, see Tokens. |
ITokenInfo example
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cC******AKBkbYjejHYX3d42VzSZ70",
"refresh_token": "a5a1ebadaba6*****27f4122",
"expires_in": 7200,
"token_type": "Bearer",
"user_id": "4b14efc*****6f63740",
"user_name": "User1",
"avatar": "",
"nick_name": "User1",
"default_drive_id": "1",
"role": "user",
"status": "enabled",
"expire_time": "2022-02-16T07:59:14Z",
"is_first_login": false,
"domain_id": "daily21453"
}
The getServiceJwtToken method
Added in v0.1.2
Gets a token for the service account of a Domain. This token grants super administrator permissions and is intended for operations management.
Note: This method applies only to Node.js.
Syntax
const result = await client.getServiceJwtToken(params, options)
Example
const domain_id = 'Your Domain ID'
const client_id = 'Your JWT application ID'
const private_key_pem = 'Your private key'
const client = new PDSClient({
auth_endpoint: `https://${domain_id}.api.aliyunpds.com`
})
const params = {
domain_id,
client_id,
private_key_pem
}
const jwtToken = await client.getServiceJwtToken(params)
Parameters
|
Field |
Type |
Required |
Description |
|
params |
IGetServiceJwtTokenReq |
Yes |
Parameters |
|
options |
AxiosRequestConfig |
Additional optional configurations. |
IGetServiceJwtTokenReq
|
Field |
Type |
Required |
Description |
|
client_id |
string |
Yes |
The JWT application ID. |
|
private_key_pem |
string |
Yes |
The private key PEM string. |
|
domain_id |
string |
Yes |
The Domain ID. |
Returns
|
Field |
Type |
Required |
Description |
|
result |
ITokenInfo |
Yes |
A token for the service account representing the Domain super administrator identity. |
The refreshJwtToken method
Added in v0.1.2
Refreshes a JWT token.
The access_token in jwtTokenInfo is valid for 2 hours, and the refresh_token is valid for 7 days.
Call this method at any time before the refresh_token expires to get a new token, regardless of whether the access_token has expired.
Refreshing jwtTokenInfo returns a new jwtTokenInfo and invalidates the old one.
const result = await client.refreshJwtToken(params, options)
Example
const client_id = 'Your JWT application ID'
const client = new PDSClient({
auth_endpoint: `https://${domain_id}.api.aliyunpds.com`
})
const params = {
client_id,
refresh_token: 'xxxxxxx'
}
const jwtToken = await client.refreshJwtToken(params)
Parameters
|
Field |
Type |
Required |
Description |
|
params |
IRefreshJwtTokenReq |
Yes |
Parameters |
|
options |
AxiosRequestConfig |
Additional optional configurations. |
IRefreshJwtTokenReq
|
Field |
Type |
Required |
Description |
|
client_id |
string |
Yes |
The JWT application ID. |
|
refresh_token |
string |
Yes |
The refresh token for the expired JWT. |
Returns
|
Field |
Type |
Required |
Description |
|
result |
ITokenInfo |
Yes |
The refreshed JWT token. |
The getTokenByCode method
Added in v0.1.2
Exchanges a one-time authorization code from the Open Authorization (OAuth) flow for a user token.
For more information about the OAuth flow, see Steps to obtain an OAuth 2.0 access token.
const result = await client.getTokenByCode(params, options)
Example
const client_id = 'Your OAuth application ID'
const client_secret = 'Your OAuth application secret'
const redirect_uri = 'Your OAuth application callback URL'
const client = new PDSClient({
auth_endpoint: `https://${domain_id}.api.aliyunpds.com`
})
const params = {
client_id,
client_secret,
redirect_uri,
code: 'xxxxxxx' // The one-time code from the OAuth application flow
}
const token = await client.getTokenByCode(params)
Parameters
|
Field |
Type |
Required |
Description |
|
params |
IGetTokenByCodeReq |
Yes |
Parameters |
|
options |
AxiosRequestConfig |
Additional optional configurations. |
IGetTokenByCodeReq
|
Field |
Type |
Required |
Description |
|
client_id |
string |
Yes |
The OAuth application ID. |
|
client_secret |
string |
The OAuth application secret. This parameter is required for web server applications. |
|
|
redirect_url |
string |
Yes |
The OAuth application callback URL. |
|
code |
string |
Yes |
The one-time code from the OAuth application flow. |
Returns
|
Field |
Type |
Required |
Description |
|
result |
ITokenInfo |
Yes |
A token representing the user identity. |
The refreshToken method
Added in v0.1.2
Refreshes a tokenInfo object from the OAuth flow.
The access_token in tokenInfo is valid for 2 hours, and the refresh_token is valid for 7 days.
Call this method at any time before the refresh_token expires to get a new token, regardless of whether the access_token has expired.
Refreshing tokenInfo returns a new tokenInfo and invalidates the old one.
const result = await client.refreshToken(params, options)
Example
const client_id = 'Your OAuth application ID'
const client_secret = 'Your OAuth application secret'
const redirect_uri = 'Your OAuth application callback URL'
const client = new PDSClient({
auth_endpoint: `https://${domain_id}.api.aliyunpds.com`
})
const params = {
client_id,
client_secret,
redirect_uri,
refresh_token: 'xxxxxxx'
}
const token = await client.refreshToken(params)
Parameters
|
Field |
Type |
Required |
Description |
|
params |
IRefreshTokenReq |
Yes |
Parameters |
|
options |
AxiosRequestConfig |
Additional optional configurations. |
IRefreshTokenReq
|
Field |
Type |
Required |
Description |
|
client_id |
string |
Yes |
The OAuth application ID. |
|
client_secret |
string |
The OAuth application secret. This parameter is required for web server applications. |
|
|
redirect_url |
string |
Yes |
The OAuth application callback URL. |
|
refresh_token |
string |
Yes |
The refresh token from a tokenInfo object. |
Return value
|
Field |
Type |
Required |
Description |
|
result |
ITokenInfo |
Yes |
The refreshed token. |