ios - 如何从 Google YouTube 数据 API OAuth 2 iOS 获取刷新 token

标签 ios oauth google-api youtube-data-api

我正在使用 OAuthSwift pod 进行 Google API 身份验证。在回调中,缺少刷新 token 。由于 Google 正在返回 500 Server Error,当用户已经通过身份验证后尝试请求新 token 时,我想存储刷新 token ,并在用户下次登录时授权用户,并检索新 token token 。

这是我的代码:

           let callback = "\(Bundle.main.bundleIdentifier ?? ""):/oauth2Callback"
            _ = ytOAuth2Swift?.authorize(
                withCallbackURL: URL(string: callback)!,
                scope: "https://www.googleapis.com/auth/youtube", state: state,
                success: { credential, response, parameters in

                   print("YouTube Access_Token \(credential.oauthToken)")
                    print("YouTube efreshR_Token \(credential.oauthRefreshToken)")
                },
                failure: { error in
                    print("ERROR: \(error.localizedDescription)")
            }
            )

这是我们得到的回复!

Response Parameters

根据 Google API 文档,这是应该出现的响应:

{
   "access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
   "expires_in":3920,
   "token_type":"Bearer",
   "refresh_token":"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
}

我尝试使用嵌入式 WebView 没有成功:

[ mbedded WebView error messageE[2]

最佳答案

好吧,看起来 google API 在开始时给了你一次刷新 token ,而新的请求不再给它了。如果是,您应该知道 refresh_token 仅在第一次授权时提供,因此如果您授权多次,它可能不会再次返回 refresh_token。请尝试按照以下说明操作:

在它之后你的下一个调用也应该返回一个 refresh_token

这可能有点不同,因为我的面板中不使用英语。当我使用 google 的 refresh_token 时,它永远不会过期,您可以永远使用它来创建新的 access_tokens。

另一种解决方案

如果它不起作用,请尝试在您的代码中将 access_type 设置为离线(我认为最简单的方法是在您的 API url 中添加一个 GET 参数作为 access_type=offline)。

必要的也可以是查询中的prompt=consent。它会强制用户授权,然后应该返回 refresh_token

更多信息可以在Google Documentation: Oauth2中找到

提示

Google API 的好处是 refresh_token 永远不会过期,除非您不撤销它或创建一个新的。因此,您甚至可以对其进行硬编码,并在每次想要获取新的 access_token 时使用。

关于ios - 如何从 Google YouTube 数据 API OAuth 2 iOS 获取刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40624926/

相关文章:

ios - Xcode 5 - CPU 和内存使用失控

asp.net - DelegatingHandler 将授权 token 添加到请求

rest - 刷新 Microsoft Live API 的 OAuth 访问 token

javascript - 谷歌折线图划分网格线

google-api - 通过 OAuth 2.0 和私钥又名服务帐户访问 Google Contacts Api

ios - 如何根据字体大小和文本计算标签的大小?

ios - Facebook SDK for IOS、Swift - 使用多个目标、不同的包标识符等

python - 为什么 Github API 允许我访问但不列出私有(private)仓库?

php - CodeIgniter-YouTube-API-Library 注册账号认证

ios - ReactiveCocoa : Chain a signal with a repeating signal