【问题标题】:Reddit OAuth2 API circular redirectReddit OAuth2 API 循环重定向
【发布时间】:2019-05-25 12:34:21
【问题描述】:

我正在尝试向 Reddit API 发送 POST 授权请求以获取访问令牌,遵循以下文档:https://github.com/reddit-archive/reddit/wiki/oauth2

执行请求时抛出以下异常:

原因:org.apache.http.client.CircularRedirectException:循环重定向到'https://www.reddit.com/api/v1/access_token'

这是使用Apache Commons HTTPKotlin代码:

@Test
fun testOauthAuthenticationManual() {

    val client = DefaultHttpClient()
    client.redirectStrategy = LaxRedirectStrategy()
    val post = HttpPost("https://www.reddit.com/api/v1/access_token")
    post.addHeader("Authorization", "Basic a3E0RWVocURGeWVoUWc6UVYyYjU0cldDeTJ4aHNZc292ZXNTcVVQc2tJ")
    post.addHeader("Content-Type", "application/x-www-form-urlencoded")
    post.addHeader("User-Agent", "Just testing")
    post.addHeader("Host", "reddit.com")

    val parameters = listOf<NameValuePair>(
            BasicNameValuePair("grant_type", "authorization_code"),
            BasicNameValuePair("redirect_uri", "http://address.co.uk"),
            BasicNameValuePair("code", "2dYqDpjs6lA7FVvUILgDaxKS2ww"))

    post.entity = UrlEncodedFormEntity(parameters, "UTF-8")

    try {
        val response = client.execute(post)
        if (response.statusLine.statusCode == 200) {
            // continue
        } else {
            throw HttpClientException(response.statusLine.reasonPhrase)
        }
    } catch (e: IOException) {
        throw HttpClientException("Could not execute HTTP request: ", e)
    }
}

我在设置请求时做错了什么?

【问题讨论】:

  • 301 更多的是信息而不是错误,您可能唯一做错的事情就是没有遵循该重定向。您可能应该检查 301 响应的 Location 字段,看看它是否符合预期(例如,您的应用程序的 redirect_uri)。
  • @Aaron 301 响应的Location 字段具有相同的uri:reddit.com/api/v1/access_token
  • 嗯,是的,这绝对很奇怪。我看不出你的代码有什么问题,我想你必须等待熟悉 reddit 的 OAuth 的人

标签: java apache http kotlin reddit


【解决方案1】:

以防其他人收到此错误 - 问题已通过删除解决:

post.addHeader("User-Agent", "Just testing")
post.addHeader("Host", "reddit.com")

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 2014-11-21
    相关资源
    最近更新 更多