0
我正在嘗試獲取GitHub oauth訪問令牌。重定向用戶到 https://github.com/login/oauth/authorize工作正常,並獲得代碼。從GitHub獲取OAuth訪問令牌時出現Cookie錯誤
然而,當我從服務器到https://github.com/login/oauth/acces_token POST請求, 服務器與
403: Forbidden/Cookies must be enabled to use GitHub.
我是不是得到了什麼在這裏迴應?這是一個API!這裏的cookies有什麼作用?我該如何解決這個錯誤?
我的代碼(使用OkHttp)
String code= ...;
HttpUrl url = new HttpUrl.Builder()
.scheme("https").host("github.com")
.addPathSegments("login/oauth/acces_token")
.build();
StringBuilder formEncoded = new StringBuilder();
formEncoded.append("client_id=").append(URLEncoder.encode(..., "UTF-8"));
formEncoded.append("&client_secret=").append(URLEncoder.encode(..., "UTF-8"));
formEncoded.append("&code=").append(URLEncoder.encode(code, "UTF-8"));
Response resp = client.newCall(
new Request.Builder().url(url)
.post(RequestBody.create(
MediaType.parse("application/x-www-form-urlencoded"),
formEncoded.toString()))
.addHeader("Accept", "application/json").build())
.execute();
if (resp.code() != HttpServletResponse.SC_OK) {
log.error("Error while getting token: {}: {}/{}",
resp.code(), resp.message(), resp.body().string());
throw new RuntimeException("Error while getting access token");
}
另請參閱http://fajitanachos.com/Authenticating-with-the-GitHub-API/和https://github.com/vjeux/GithubLogin/blob/master/token.php – ruediste