Swift Alamofire从刷新令牌请求错误获取令牌 [英] Swift Alamofire Getting Token from Refresh Token Request Error

查看:393
本文介绍了Swift Alamofire从刷新令牌请求错误获取令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用Alamofire还是很陌生,并且我正按照这个要求将我的头撞在墙上。我正在使用GIDSignIn,并成功为用户获取了令牌并刷新了令牌,其范围为[ https://www.googleapis.com/auth/youtube.readonly ]。

I'm pretty new to using Alamofire, and I am banging my head against the wall with this request. I'm using GIDSignIn, and successfully get a token and refresh token for the user, with the scope ["https://www.googleapis.com/auth/youtube.readonly"].

我正在尝试完成此请求,如网站。该网站说要忽略对iOS使用 client_secret 的操作。

I'm trying to complete this request, as shown as an example on the site. The site says to ignore using client_secret for iOS, which I do.

POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded

client_id=<your_client_id>&
client_secret=<your_client_secret>&
refresh_token=<refresh_token>&
grant_type=refresh_token

下面是我如何在Alamofire中实现它。我的 client_id 是GoogleService-Info.Plist中CLIENT_ID密钥中的值,该字符串以.apps.googleusercontent.com结尾。从我在网上看到的其他示例来看, refresh_token 似乎也具有正确的格式。

Below is how I've implemented it with Alamofire. My client_id is the value from the CLIENT_ID key in the GoogleService-Info.Plist, a string ending in .apps.googleusercontent.com. The refresh_token also seems to have the right format from other examples I've seen online.

let endpoint = "https://www.googleapis.com/oauth2/v4/token"
let parameters = [
    "client_id" : client_id, 
    "refresh_token" : refresh_token, 
    "grant_type" : "refresh_token"
]

Alamofire.request(endpoint, method: .post, 
                  parameters: parameters, 
                  encoding: JSONEncoding.default)

        .responseJSON { (data) in
            print("data: \(data)")
            let json = JSON(data.result)
        }

数据响应为

data: SUCCESS: {
    error = "unsupported_grant_type";
    "error_description" = "Invalid grant_type: ";
}

并非十分成功。我是否需要以其他方式配置我的请求,或者获得适当的访问/许可以获取令牌?

Not terribly successful. Do I need to configure my request differently, or get appropriate access / permission to get the token? Thank you so much!

推荐答案

@BikeshThakur帮我弄清楚了! URLEncoding.httpBody做到了!我也不需要任何标题。

@BikeshThakur helped me figure it out! The URLEncoding.httpBody did the trick! I don't need any headers either.

Alamofire.request(endpoint, method: .post, 
                  parameters: parameters, 
                  encoding: URLEncoding.httpBody)

这篇关于Swift Alamofire从刷新令牌请求错误获取令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆