JAVA OkHttpClient身份验证器不适用于Bitbucket API

时间:2016-06-15 11:17:51

标签: java rest authentication curl

如果我使用 OkHttpClient 身份验证示例(页面末尾为https://github.com/square/okhttp/wiki/Recipes),我就成功了。但是,如果我尝试对 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();

如果我使用卷曲而不是它的作品! - 简短版本:

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

任何建议?

1 个答案:

答案 0 :(得分:0)

好的 - 我明白了 - 我必须为每个请求添加身份验证凭据:

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();