Updated on 2023-11-23 GMT+08:00

Creating OAuth Authorization

Function

This API is used to create OAuth authorization.

URI

POST /v1/{project_id}/git/auths/{repo_type}/oauth

Table 1 describes the parameters.

Table 1 Path parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Tenant's project ID. See Obtaining a Project ID.

repo_type

Yes

String

Repository type.

Value: GitHub, BitBucket, or GitLab.

Table 2 Query parameters

Parameter

Mandatory

Type

Description

tag

No

String

Site label. Example: ?tag=intl, which indicates an international site.

Request

Table 3 Request header parameters

Parameter

Mandatory

Type

Description

Content-Type

Yes

String

Message body type or format.

X-Auth-Token

Yes

String

API calling can be authenticated using a token or AK/SK. If you use a token, this parameter is mandatory and must be set to the token. For details about how to obtain a token, see Obtaining a User Token.

Table 4 Request body parameters

Parameter

Mandatory

Type

Description

name

Yes

String

Authorization name.

code

Yes

String

code parameter in the redirection URL after Git repository authorization is performed.

state

Yes

String

One-off authentication code and random string received after Git repository authorization is performed.

To obtain the values of code and state, perform the following steps:

  1. Enter the URL obtained in Obtaining an Authorization Redirection URL in the address box of the browser and press Enter.
  2. On the login page, enter your code repository account and password.

    If you have logged in to the code repository, skip this step.

  3. On the login page of the Huawei Cloud console, enter the account name and password, and click Log In.

    If you have logged in to the Huawei Cloud console, skip this step.

  4. After the system displays a message indicating that the authorization fails to be created, copy the URL in the address box.

    For example:

    https://console.huaweicloud.com/servicestage/oauth?repo_type=github&code=04bed2df009f3fc9***c&state=%242a%2410%24.uQYThj5yKFs51w9s9ajeuQ0NSGshPH7IUfXoZLPlz7J%2FoAJ8B***

  5. Decode the obtained URL to obtain the decoded URL.

    For example:

    https://console.huaweicloud.com/servicestage/oauth?repo_type=github&code=04bed2df009f3fc9***c&state=$2a$10$.uQYThj5yKFs51w9s9ajeuQ0NSGshPH7IUfXoZLPlz7J/oAJ8B***

    Obtain the values of code and state.

The value of the obtained state is a one-off authentication code and random string, which can be used only once. If you need to call this API again to create OAuth authorization, follow the steps in Obtaining an Authorization Redirection URL again to obtain the URL and obtain the values of code and state.

Response

Table 5 Response parameters

Parameter

Type

Description

authorization

Object

Authorization structure.See Table 6.

Table 6 authorization parameters

Parameter

Type

Description

name

String

Authorization name.

repo_type

String

Repository type.

Value: github, gitlab, , or bitbucket.

repo_host

String

Repository address.

repo_home

String

Repository homepage.

repo_user

String

Repository username.

avartar

String

Repository avatar.

token_type

String

Repository authorization type.

create_time

Long

Repository authorization creation timestamp.

update_time

Long

Repository authorization update timestamp.

status

Integer

Repository authorization status.

0: normal

1: overdue

2: unauthenticated

Example Request

Create OAuth authorization auth1.

{
  "name": "auth1",
  "code": "04bed2df009f3fc9***c",
  "state": "$2a$10$.uQYThj5yKFs51w9s9ajeuQ0NSGshPH7IUfXoZLPlz7J/oAJ8B***"
}

Example Response

{
  "authorization": {
    "name": "auth1",
    "repo_type": "github",
    "repo_host": "https://api.github.com",
    "repo_home": "https://github.com/tom-repo",
    "repo_user": "repo",
    "avartar": "https://avatars.githubusercontent.com/u/73919200?v=4",
    "token_type": "oauth",
    "create_time": 1649758626858,
    "update_time": 1649758626858,
    "status": 0
  }
}

Status Code

Table 7 Status codes

HTTP Status Code

Description

200

OK

400

Bad Request

Error Code

The error code format is SVCSTG.REPO.[Error_ID], for example, SVCSTG.REPO.0401. For details, see ServiceStage Error Codes.