如何从 Google YouTube 数据 API OAuth 2 iOS 获取刷新令牌
How to get a refresh token from Google YouTube Data API OAuth 2 iOS
我正在使用 OAuthSwift
pod 进行 Google API 身份验证。在回调中,缺少刷新令牌。由于 Google 正在返回 500 Server Error
,当用户已经通过身份验证后尝试请求新令牌时,我想存储刷新令牌,并在用户下次登录时授权用户,并检索新令牌.
这是我的代码:
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)")
}
)
这是我们得到的回复!
根据 Google API 文档,这是应该出现的响应:
{
"access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
"expires_in":3920,
"token_type":"Bearer",
"refresh_token":"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
}
我尝试使用嵌入式 WebView
运气不好:
[
嗯,看起来 google API 一开始给了你一次刷新令牌,新的请求不再给它了。如果是,您应该知道 refresh_token 仅在第一次授权时提供,因此如果您授权多次,它可能不会再次 return 和 refresh_token。请尝试按照以下说明操作:
- 转到您的帐户安全设置:https://www.google.com/settings/u/1/security。
- 单击 "Authorizing applications and sites" 旁边的编辑按钮。
- 然后点击您的应用旁边的 "Revoke Access"。
在它之后你下次调用也应该 return a refresh_token
可能有点不同,因为我的面板中没有使用英语。当我从 google 使用 refresh_token 时,它永远不会过期,您可以永远使用它来创建新的 access_tokens.
另一种解决方案
如果它不起作用,请尝试在您的代码中将 access_type 设置为离线(我认为最简单的方法是在您的 API url 中添加一个 GET 参数如 access_type=offline
)。
需要也可以prompt=consent
查询。它会强制用户授权,然后应该 return refresh_token
更多信息可以在Google Documentation: Oauth2
中找到
提示
Google API 的好处是 refresh_token 永远不会过期,直到您不会撤销它或创建一个新的。因此,您甚至可以对其进行硬编码,并在每次想要获得新的 access_token.
时使用
我正在使用 OAuthSwift
pod 进行 Google API 身份验证。在回调中,缺少刷新令牌。由于 Google 正在返回 500 Server Error
,当用户已经通过身份验证后尝试请求新令牌时,我想存储刷新令牌,并在用户下次登录时授权用户,并检索新令牌.
这是我的代码:
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)")
}
)
这是我们得到的回复!
根据 Google API 文档,这是应该出现的响应:
{
"access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
"expires_in":3920,
"token_type":"Bearer",
"refresh_token":"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
}
我尝试使用嵌入式 WebView
运气不好:
[
嗯,看起来 google API 一开始给了你一次刷新令牌,新的请求不再给它了。如果是,您应该知道 refresh_token 仅在第一次授权时提供,因此如果您授权多次,它可能不会再次 return 和 refresh_token。请尝试按照以下说明操作:
- 转到您的帐户安全设置:https://www.google.com/settings/u/1/security。
- 单击 "Authorizing applications and sites" 旁边的编辑按钮。
- 然后点击您的应用旁边的 "Revoke Access"。
在它之后你下次调用也应该 return a refresh_token
可能有点不同,因为我的面板中没有使用英语。当我从 google 使用 refresh_token 时,它永远不会过期,您可以永远使用它来创建新的 access_tokens.
另一种解决方案
如果它不起作用,请尝试在您的代码中将 access_type 设置为离线(我认为最简单的方法是在您的 API url 中添加一个 GET 参数如 access_type=offline
)。
需要也可以prompt=consent
查询。它会强制用户授权,然后应该 return refresh_token
更多信息可以在Google Documentation: Oauth2
中找到提示
Google API 的好处是 refresh_token 永远不会过期,直到您不会撤销它或创建一个新的。因此,您甚至可以对其进行硬编码,并在每次想要获得新的 access_token.
时使用