Java Okhttpclient身份验证器与Bitbucket API不适合我



如果我使用 authentication okhttpclient 的示例如上所述(https://github.com/square/square/okhttp/wiki/页面末尾的食谱)我成功了。但是,如果我尝试为 bitbucket api (版本2,云)进行身份验证,我得到:

java.io.IOException: Unexpected code Response{protocol=http/1.1, code=403, message=FORBIDDEN, url=https://api.bitbucket.org/2.0/repositories/myuser/test/pullrequests}

我尝试了:

Request request = new Request.Builder()
.url("https://api.bitbucket.org/2.0/repositories/myuser/test/pullrequests")
.get()
.build();

如果我使用 curl 而是工作! - 简短版本:

    String command = "curl -u myuser:mypass -X GET https://api.bitbucket.org/2.0/repositories/myuser/test/pullrequests");
    p = Runtime.getRuntime().exec(command);

任何建议?

好的 - 我明白了 - 我必须将身份证书添加到每个请求:

final String login = "myuser";
final String password = "mypass";
String credential = Credentials.basic(login, password);
Request request = new Request.Builder()
.url("https://api.bitbucket.org/2.0/repositories/myuser/test/pullrequests")
.header("Authorization", credential)
.get()
.build();

最新更新