10

I am trying to automate the app publishing process to the Huawei store using the REST APIs as mentioned in the link. https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agcapi-overview

I successfully received an access token but other operations(ex: getting the app info, getting the upload URL) are getting failed with the below status code and error.

403 client token authorization fail.

I did not write any code, I simply used the below sample code and updated clientId, clientSecret, appId.

https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Examples/agcapi-publish_api_code

What could go wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Ponsuyambu
  • 7,876
  • 5
  • 27
  • 41
  • was you able to resolve the issue? We have the same problem where the token is received successfully, but then we get 403 error. – Vladimir Ryhlitskiy Oct 06 '20 at 15:48
  • Yeah see this comment. https://stackoverflow.com/questions/63999681/huawei-appgallery-connect-api-403-client-token-authorization-fail/64002280#comment113182184_64002280 Once I set the project to NA it is started working. – Ponsuyambu Oct 06 '20 at 19:15
  • I have tried it on Postman and it works – Hunter Oct 07 '20 at 07:40

2 Answers2

9

Update:

  • Set Project to N/A to define the API client as a team-level one.
  • Set Roles to Administrator
  1. Please check whether your client role is Administrator.

The role of a member determines the permissions in AppGallery Connect. Administrator has most operation permissions, being able to add member accounts and assigning permissions to them. For details about the mapping between roles and permissions, please refer to Roles and Permissions.

  1. Work with AppGallery Connect API

To call the AppGallery Connect API, you need to obtain authorization from the AppGallery Connect server in advance using either of the following modes: API client mode and OAuth client mode. To call the AppGallery Connect API in the API client mode, you need to manage your API client in AppGallery Connect. An API client can be managed only by your team account holder. The basic process is as follows:

A. Creating an API Client

  • Sign in to AppGallery Connect and select Users and permissions.
  • Go to Api Key > AppGalleryConnect API from the navigation tree on the left and click Create.
  • Set Name to a customized client name, set Roles to the corresponding role, and click Confirm.
  • After the client is successfully created, record the values of Client ID and Key in the client information list.

Check the screenshot below: client information

B. Obtaining the Token for Accessing the API

After an API client is created, the API client needs to be authenticated in AppGallery Connect. After the authentication is successful, the API client obtains an access token for accessing the AppGallery Connect API. With this access token, you can access the AppGallery Connect API.

To obtain an access token, you need to add the code for calling the Obtaining a Token API to your app program.

public static String getToken(String domain, String clientId, String clientSecret) {

    String token = null;

    try {

        HttpPost post = new HttpPost(domain + "/oauth2/v1/token");

        JSONObject keyString = new JSONObject();

        keyString.put("client_id", "18893***83957248");

        keyString.put("client_secret", "B15B497B44E080EBE2C4DE4E74930***52409516B2A1A5C8F0FCD2C579A8EB14");

        keyString.put("grant_type", "client_credentials");

        StringEntity entity = new StringEntity(keyString.toString(), Charset.forName("UTF-8"));

        entity.setContentEncoding("UTF-8");

        entity.setContentType("application/json");

        post.setEntity(entity);

        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpResponse response = httpClient.execute(post);

        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode == HttpStatus.SC_OK) {

            BufferedReader br =

                new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Consts.UTF_8));

            String result = br.readLine();

            JSONObject object = JSON.parseObject(result);

            token = object.getString("access_token");

        }

        post.releaseConnection();

        httpClient.close();

    } catch (Exception e) {

    }

    return token;

}

After obtaining an access token, you can use the access token for identity authentication when accessing the AppGallery Connect API. The default validity period of an access token is 48 hours. If an access token expires, you need to obtain a new access token.

C. Accessing the API

After obtaining an access token, you can use the access token to call the AppGallery Connect API to complete function development.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • I already have done everything you've mentioned. There is only one user in the account. And when I created API client I made sure that role is set to Administrator. As I said earlier I obtained the token successfully, only the subsequent calls are failing though I attached the token in the header. – Ponsuyambu Sep 22 '20 at 07:02
  • 5
    @PonsuyambuVelladurai Please check whether you set **Project** to N/A to define the API client as a team-level one. If you have any more questions, feel free to contact me. :) – zhangxaochen Sep 22 '20 at 08:16
  • @PonsuyambuVelladurai Okay, I updated my answer for both the project and roles needs to be checked. – zhangxaochen Oct 10 '20 at 01:05
  • 1
    Stupid question but @shirley where I can set this? Thanks a lot. EDIT: Stupid me, in step when you are creating Api Key > AppGalleryConnect API – Nihad Delic Nov 02 '20 at 14:51
5

As said in this comment, Once I set the project to NA it started working.

Thanks to @shirley

Ponsuyambu
  • 7,876
  • 5
  • 27
  • 41