用java实现在gitee上创建仓库的代码
时间: 2023-04-05 21:00:52 浏览: 170
您好,以下是用 Java 实现在 Gitee 上创建仓库的代码示例:
```java
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class GiteeRepoCreator {
private static final String GITEE_API_URL = "https://gitee.com/api/v5/user/repos";
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
public void createRepo(String accessToken, String repoName) throws IOException {
String json = "{\"name\":\"" + repoName + "\"}";
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(GITEE_API_URL)
.header("Authorization", "token " + accessToken)
.post(body)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("Failed to create repository: " + response);
}
}
}
```
以上代码使用了 OkHttp 库来发送 HTTP 请求,其中 `accessToken` 是 Gitee 的访问令牌,`repoName` 是要创建的仓库名称。调用 `createRepo` 方法即可创建仓库。
阅读全文