代码之家  ›  专栏  ›  技术社区  ›  crmepham

Reddit OAuth2 API循环重定向

  •  1
  • crmepham  · 技术社区  · 6 年前

    我试图发送一个 POST 根据以下文档,向Reddit API发出获取访问令牌的授权请求: https://github.com/reddit-archive/reddit/wiki/oauth2

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

    这是 Kotlin 使用的代码 Apache Commons HTTP

    @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)
        }
    }
    

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   crmepham    6 年前

    如果其他人收到此错误,则通过删除以下项解决此问题:

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