Obtain an access token

Updated at:

Before you begin

  1. Create an Alibaba Cloud account, pass real-name verification, create a Secure Authentication instance, and then create an application for the instance.

  2. Obtain an Alibaba Cloud AccessKey pair.

Obtain an application authorization token

Obtains an access token that is required for a mobile client SDK to call API operations.

Request parameters

Name

Type

Required

Sample value

Description

ApplicationExternalId

String

Yes

A0000001

The application ID. The ID is specified when you create the application. You can view the ID in the details panel of the application.

MobileExtendParamsJson

String

Yes

-

The data generated by calling the mobile SDK. The data is a Base64-encoded JSON string. For more information, see iOS SDK integration and Android SDK integration.

MobileExtendParamsJsonSign

String

Yes

5724badc40b93c1748803611abd591832e2f029979f0d46f0eeef42360dad5cb

The application signature data generated by calling the mobile SDK. The data is a hexadecimal encoded string.

UserId

String

Yes

zhangsan

The ID of the test user.

XClientIP

String

No

10.11.100.10

The IP address of the mobile client. We recommend that you specify this parameter. The Secure Authentication service performs intelligent security risk control based on this parameter.

Returned Data

Name

Type

Example

Description

Success

boolean

true

The result of the operation. A value of true indicates success. A value of false indicates failure.

Code

String

Operation.Success

The response code.

  • If Success is true, the value is Operation.Success.

  • If Success is false, the Code parameter returns an error code as described below.

Message

String

Operation.Success

The description. If Success is false, this parameter provides details about the error code.

RequestId

String

1C0EE50A-B3BB-42FD-AB59-E3FE88976982

The request ID.

Data

String

-

If the request was successful, the JSON string that is returned contains the access_token parameter.

  • access_token: A string that the mobile client uses to call mobile number authentication and Internet Finance Authentication Alliance (IFAA) authenticator services.

If the request failed, no value is returned.

Examples

Request method: POST

https://idaas-doraemon.aliyuncs.com/?Action=FetchAccessToken
&Version=2021-05-20
&ApplicationExternalId=A0000001
&MobileExtendParamsJson=eyJhcHBJ...biI6IjEuMCJ9
&MobileExtendParamsJsonSign=5724badc40b...6f0eeef42360dad5cb
&UserId=zhangsan
&<Common request parameters>

Successful response example

{
  "Code":"Operation.Success",
    "Data":{
      "ExpiresIn":"86399",
      "Scope":"read",
      "AccessToken":"xxxxx",
      "TokenType":"bearer"
    },
  "RequestId":"71A9178E-7D40-5CFE-8441-9C14ECEE6E71",
  "Success":true,
  "Message":"Operation.Success"
}

Sample error responses: The specified application ID does not exist.

{
  "Success": false,
  "Code": "Operation.Failure.Application.ResourceNotExist",
  "Message": "APIInvokeError.ApplicationDoesNotExist",
  "RequestId": "1C0EE50A-B3BB-42FD-AB59-E3FE88976982",
  "Data":null
}

Sample code in Java

The following sample code provides an example on how to obtain an access token:

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.idaas_doraemon.model.v20210520.ServiceInvokeRequest;
import com.aliyuncs.idaas_doraemon.model.v20210520.ServiceInvokeResponse;
import com.aliyuncs.profile.DefaultProfile;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class IDaaSAuthSample {

    /**
     * Use an AccessKey pair to initialize the client.
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static IAcsClient createClient(String accessKeyId, String accessKeySecret) throws Exception {
      // An AccessKey pair of an Alibaba Cloud account has permissions on all API operations. This poses a high security risk. We strongly recommend that you create and use a Resource Access Management (RAM) user for API access or routine O&M. To create a RAM user, log on to the RAM console.
        // This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
        // To prevent AccessKey pair leaks, do not hardcode the AccessKey ID and AccessKey secret in your code.
      	String accessKeyId = System.getenv("ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ACCESS_KEY_SECRET");
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",
                accessKeyId,
                accessKeySecret);
        // addEndpoint
        DefaultProfile.addEndpoint("cn-hangzhou",
                "idaas-doraemon",
                "idaas-doraemon.aliyuncs.com");
        // The domain name to access.
        return new DefaultAcsClient(profile);
    }

    public static void main(String[] args_) throws Exception {
        // Obtain the token.
        fetchAccessToken();
    }

    public static void fetchAccessToken() throws Exception {
       IAcsClient client = IDaaSAuthSample.createClient("Your AccessKey", "Your AccessSecret");
       // Obtain the application authorization token.
       FetchAccessTokenRequest request = new FetchAccessTokenRequest();
       request.setApplicationExternalId("testApplication");
       request.setMobileExtendParamsJson("eyJhcHBJZCI6....YXBwT1MiOiJpT1Mi");
       request.setMobileExtendParamsJsonSign("ba6f5596f00102cb1a7d971f8390b7ccb28e6bc4e8694d051f9299ccc69e1186");
       request.setUserId("zhangsan");
       // If you copy the code to run it, print the return value of the API call.
       try {
           FetchAccessTokenResponse response = client.getAcsResponse(request);
           System.out.println(response.getData());
       } catch (Exception e) {
            // Determine the cause of the exception from e.getCode().
            System.out.println(e);
       }
    }
}

Maven dependencies of the sample code

  <dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-idaas-doraemon</artifactId>
   <version>1.2.4</version>
  </dependency>
<dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-core</artifactId>
   <optional>true</optional>
   <version>[4.4.9,5.0.0)</version>
  </dependency>