Reddit OAuth2 API circular redirect

问题: I am attempting to send a POST authorization request to the Reddit API to get an access token, following this documentation: https://github.com/reddit-archive/reddit/wiki/o...

问题:

I am attempting to send a POST authorization request to the Reddit API to get an access token, following this documentation: https://github.com/reddit-archive/reddit/wiki/oauth2

I am getting the following exception thrown when I execute the request:

Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'https://www.reddit.com/api/v1/access_token'

Here is the Kotlin code which uses 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)
    }
}

What am I doing wrong in setting up my request?


回答1:

Incase anyone else gets this error - the issue was fixed by removing:

post.addHeader("User-Agent", "Just testing")
post.addHeader("Host", "reddit.com")
  • 发表于 2018-12-29 14:23
  • 阅读 ( 270 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除