android - Huawei AppGallery Connect API - 403 客户端 token 授权失败

标签 android huawei-mobile-services huawei-developers appgallery

我正在尝试使用链接中提到的 REST API 将应用程序发布过程自动化到华为商店。
https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agcapi-overview
我成功收到了访问 token ,但其他操作(例如:获取应用程序信息、获取上传 URL)因以下状态代码和错误而失败。

403 client token authorization fail.
我没有写任何代码,我只是使用下面的示例代码并更新了clientId、clientSecret、appId。
https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Examples/agcapi-publish_api_code
会出什么问题?

最佳答案

更新:

  • 设置 项目 不适用 将 API 客户端定义为团队级别的客户端。
  • 设置 角色 管理员
  • 请检查您的客户角色是否为管理员 .

  • 成员的角色决定了 AppGallery Connect 中的权限。管理员拥有大部分操作权限,可以添加成员账户并为其分配权限。角色与权限的对应关系请引用Roles and Permissions .
  • 使用 AppGallery Connect API

  • 调用AppGallery Connect API,您需要提前通过AppGallery Connect服务器获取授权,有两种方式:API客户端方式和OAuth客户端方式。以API客户端方式调用AppGallery Connect API,您需要在AppGallery Connect中管理您的API客户端。 API 客户端只能由您的团队帐户持有人管理。基本流程如下:
    A. 创建 API 客户端
  • 登录 AppGallery Connect并选择 用户和权限 .
  • 转至 API key > AppGalleryConnect API 在左侧导航树中点击创建 .
  • 将 Name 设置为自定义的客户端名称,设置 角色 到对应的角色,点击确认 .
  • 客户端创建成功后,记录的值客户 ID key 在客户信息列表中。

  • 检查下面的屏幕截图:
    client information
    B.获取访问API的Token
    创建 API 客户端后,需要在 AppGallery Connect 中对 API 客户端进行认证。认证成功后,API客户端获取访问AppGallery Connect API的访问 token 。使用此访问 token ,您可以访问 AppGallery Connect API。
    要获取访问 token ,您需要添加调用 Obtaining a Token 的代码应用程序的 API。
    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;
    
    }
    
    获取访问 token 后,您可以在访问AppGallery Connect API时使用访问 token 进行身份认证。访问 token 的默认有效期为 48 小时。如果访问 token 过期,您需要获取新的访问 token 。
    C. 访问 API
    获取访问 token 后,您可以使用访问 token 调用AppGallery Connect API完成功能开发。

    关于android - Huawei AppGallery Connect API - 403 客户端 token 授权失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63999681/

    相关文章:

    android - 何时使用自定义 CursorLoader?

    java - 无法保存 Eclipse .java 和 .xml 文件中的更改

    android - 我可以为以编​​程方式构造的可绘制对象分配一个 id 吗?

    android - 华为 P40 Pro (ELS-NX9) 上的 INSTALL_FAILED_ABORTED 错误

    flutter - HMS Core Playstore 版本无法使用华为帐号登录

    android - 如何接收华为运动健康App的数据?

    java - Android ScrollView 不以相对布局滚动

    notifications - 如果应用程序是从APPGALLERY安装的,HMS getToken返回null

    android - 为华为应用程序库实现评价我们和更多应用程序链接

    huawei-mobile-services - json int数据类型自动变为double