文档

快速入门

更新时间:

本章节以CreateRepository为例,为你演示如何通过 OpenAPI Explorer 和阿里云 SDK 等开发者工具调用 Codeup API。

背景信息

调用 API 时,你可以根据 API 文档了解使用说明,并查询必选的请求参数。发送请求后报错时,你可以在相应 API 文档中获取错误码说明。

调用方式

  • OpenAPI Explorer示例:适用于习惯交互式操作界面的场景,或者初次使用阿里云产品的开发者用户。你可以在OpenAPI Explorer中调试和获取SDK请求示例。更多有关OpenAPI Explorer的详情,请参见 什么是OpenAPI Explorer

  • Java SDK示例:适用于SDK编码或DevOps等场景。要求你已提前安装了Codeup Java SDK,安装方式请参见「SDK 概述」章节

OpenAPI Explorer 示例

Codeup OpenAPI Explorer 地址:https://api.aliyun.com/#/?product=codeup

  1. 在 devops 企业管理后台获取企业 ID

1

w2. 参考 创建代码库 调用 CreateRepository 接口

Java SDK 示例

示例代码中的下列参数需要您根据实际情况自行填写。

  • <AccessKey>:您的AccessKeyId。获取方式请参见 获取AccessKey

  • <AccessSecret>:您的AccessKeySecret。

  • <RegionId>:Codeup 地域 ID,目前只支持cn-hangzhou。

  • <OrganizationId>:企业ID,到企业管理后台获取。

package com.alibaba.openapitest.demo;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.codeup.model.v20200414.CreateRepositoryRequest;
import com.aliyuncs.codeup.model.v20200414.CreateRepositoryResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;

public class CreateRepository {

    private String accessKeyId = "<AccessKey>";
    private String accessSecret = "<AccessSecret>";

    /**
     * 个人访问令牌;使用AK&SK或STS 临时授权方式不传该字段
     */
    private String personalAccessToken = "<PersonalAccessToken>";

    private String regionId = "cn-hangzhou";
    private String endPoint = "codeup.cn-hangzhou.aliyuncs.com";

    /**
     * 企业 ID
     */
    private String organizationId = "<OrganizationId>";

    public void createRepository() {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CreateRepositoryRequest request = new CreateRepositoryRequest();
        request.setEndpoint(endPoint);
        request.setOrganizationId(organizationId);
        request.setAccessToken(personalAccessToken);

        // 请求体参数,参考API文档
        String body = "{\"name\": \"repoName\", \"path\": \"repoPath\", \"visibility_level\": 10, \"namespace_id\": 123}";

        request.setHttpContent(JSON.toJSONString(body).getBytes(), "Utf-8", FormatType.JSON);
        try {
            CreateRepositoryResponse response = client.getAcsResponse(request);
            logInfo(String.valueOf(response.getResult().getId()));
        } catch (ServerException e) {
            logInfo(String.format("Fail. Something with your connection with Aliyun go incorrect. ErrorCode: %s",
                    e.getErrCode()));
        } catch (ClientException e) {
            logInfo(String.format("Fail. Business error. ErrorCode: %s, RequestId: %s",
                    e.getErrCode(), e.getRequestId()));
        }
    }

    private static void logInfo(String message) {
        System.out.println(message);
    }

    public static void main(String[] args) {
        new CreateRepository().createRepository();
    }
}
  • 本页导读 (0)
文档反馈