Updated on 2024-12-30 GMT+08:00

Making an API Request

This section describes the structure of an API request, and uses the OneAccess API used to obtain an access token for OAuth 2.0-based application authentication as an example to demonstrate how to call an API. The obtained access token can then be used to authenticate the calling of the API used to query user information.

Prerequisites

Before calling APIs, obtain ClientId and ClientSecret of added applications in the administrator portal from Basic Information.

Request URI

A request URI is in the following format:

{URI-scheme} :// {domain_name} / {resource-path} ? {query-string}

Although a request URI is included in the request header, most programming languages or frameworks require the request URI to be transmitted separately.

  • URI-scheme: Protocol used to transmit requests. All APIs use HTTPS.
  • domain_name: User access domain name that you must specify when you call a OneAccess API. For details about how to obtain the domain name, see Obtaining the User Access Domain Name.
  • resource-path: Access path of an API for performing a specified operation. Obtain the path from the URI of an API. For example, the resource-path of the API used to obtain a user token is api/v1/oauth2/token.
  • query-string: An optional query parameter. Ensure that a question mark (?) is included before each query parameter that is in the format of "Parameter name=Parameter value". For example, ? limit=10 indicates that a maximum of 10 data records will be displayed.

For example, to obtain an access token for the domain name abcdefg.huaweioneaccess.com, provide this domain name and obtain the resource-path (/api/v1/oauth2/token) in the URI of the API used to obtain an access token. Then, construct the URI as follows:

https://abcdefg.huaweioneaccess.com/api/v1/oauth2/token
Figure 1 Example URI

To simplify the URI display, each API is provided only with a resource-path and a request method. The URI-scheme of all APIs is HTTPS, and the domain_name values of all APIs in the same instance are identical.

Request Methods

The HTTP protocol defines the following request methods that can be used to send a request to the server.

  • GET: requests the server to return specified resources.
  • PUT: requests the server to update specified resources.
  • POST: requests the server to add resources or perform special operations.
  • DELETE: requests the server to delete specified resources, for example, an object.
  • HEAD: requests the server to return the response header only.
  • PATCH: requests the server to update partial content of a specified resource. If the resource does not exist, a new resource will be created.

For example, in the case of the API used to obtain an access token, the request method is POST. The request is as follows:

POST https://abcdefg.huaweioneaccess.com/api/v1/oauth2/token

Request Header

You can also add additional fields to a request, such as the fields required by a specified URI or HTTP method. For example, to request for the authentication information, add Content-Type, which specifies the request body type.

Table 1 lists the common request header fields.
Table 1 Common request header fields

Name

Description

Mandatory

Example

Content-Type

Specifies the request body type or format. The default value is application/json. Other values of this field will be provided for specific APIs if any.

Yes

application/json

Accept

Response type. The default value application/json is recommended. Other values of this field will be provided for specific APIs if any.

No

application/json

Content-Length

Length of the request body, in bytes.

No

3495

The API used to obtain an access token does not require authentication. Therefore, only the Content-Type field needs to be added to requests for calling the API. An example of such requests is as follows:

POST https://abcdefg.huaweioneaccess.com/api/v1/oauth2/token
Content-Type: application/json

(Optional) Request Body

This part is optional. The body of a request is often sent in a structured format (for example, JSON or XML) as specified in the Content-Type header field. The request body transfers content except the request header.

The request body varies between APIs. Some APIs do not require the request body, such as the APIs requested using the GET and DELETE methods.

In the case of the API used to obtain an access token, the request parameters and parameter description can be obtained from the API request. The following provides an example request with a body included. Replace domain_name (user access domain name), ****** (authorization code returned after login), ClientId (API credential ID generated after application registration), ClientSecret (API credential secret generated after application registration), and https://example.com (callback address of an application specified during registration) with the actual values.

POST https://abcdefg.huaweioneaccess.com/api/v1/oauth2/token
Content-Type: application/json

code=******&
client_id=ClientId&
client_secret=ClientSecret&
redirect_uri=https://example.com&
grant_type=authorization_code

If all data required for the API request is available, you can send the request to call the API through curl, Postman, or coding. In the response to the API used to obtain an access token, access_token is the desired access token. This token can then be used to authenticate the calling of other APIs.