【发布时间】:2017-12-10 22:50:35
【问题描述】:
我正在开发一个需要使用 Youtube Data API 上传视频的 iOS 移动应用程序。总的来说,我了解所需的工作流程:
1) 使用 clientId、clientSecret 和其他详细信息向身份验证端点发送请求。
2) 服务对客户端进行身份验证,并将请求发送回客户端指定的包含访问令牌的回调 URL。
3) 客户端在他/她想要发送未来请求时在标头中提供令牌。
我已经使用 node.js 脚本成功上传了 Youtube 视频,但是我在理解如何在 Swift 中实现它时遇到了很多麻烦。在我的 VideoManagementController 中,我有一个按钮可以触发 sample.mp4 文件的上传:
let headers = ["Authorization": "Bearer \(self.newToken))"]
let path = Bundle.main.path(forResource: "sample", ofType: ".mp4")
let videodata : NSData = NSData.dataWithContentsOfMappedFile(path!)! as! NSData
print("TOKEN: \(String(describing: token))")
Alamofire.request("https://www.googleapis.com/upload/youtube/v3/videos?part=id", method: .post, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
print(response)
}
我正在尝试在控制器的 viewDidLoad() 阶段检索我的访问令牌:
let authorizationEndpoint = URL(string: "https://accounts.google.com/o/oauth2/v2/auth")
let tokenEndpoint = URL(string: "https://www.googleapis.com/oauth2/v4/token")
let configuration = OIDServiceConfiguration(authorizationEndpoint: authorizationEndpoint!, tokenEndpoint: tokenEndpoint!)
let request = OIDAuthorizationRequest(configuration: configuration, clientId: self.kClientID, scopes: [OIDScopeOpenID, OIDScopeProfile], redirectURL: self.KRedirectURI!, responseType: OIDResponseTypeCode, additionalParameters: nil)
let appDelegate: AppDelegate? = (UIApplication.shared.delegate as? AppDelegate)
print("REACHED")
appDelegate?.currentAuthorizationFlow = OIDAuthState.authState(byPresenting: request, presenting: self, callback: {(_ authState: OIDAuthState?, _ error: Error?) -> Void in
if (authState != nil) {
print("TOKEN: Got authorization tokens. Access token: \(String(describing: authState?.lastTokenResponse?.accessToken))")
self.newToken = authState?.lastTokenResponse?.accessToken
self.authState = authState
}
else {
print("TOKEN: Authorization error: \(String(describing: error?.localizedDescription))")
self.authState = nil
}
})
问题是我的访问令牌检索代码基本上挂起并且永远不会完成。它到达了打印语句"REACHED",但从未出现在以下代码部分中:
appDelegate?.currentAuthorizationFlow = OIDAuthState.authState(byPresenting: request, presenting: self, callback: {(_ authState: OIDAuthState?, _ error: Error?) -> Void in
if (authState != nil) {
print("TOKEN: Got authorization tokens. Access token: \(String(describing: authState?.lastTokenResponse?.accessToken))")
self.newToken = authState?.lastTokenResponse?.accessToken
}
else {
print("TOKEN: Authorization error: \(String(describing: error?.localizedDescription))")
self.authState = nil
}
我既没有获得新令牌,也没有打印出authorization error。
我怀疑这与我的回调 URL 有关。我认为 Swift 不知道从哪里“听”来获取我的访问令牌。例如,在 node.js 中设置回调 URL 端点相对容易,但我如何在 Swift 中做到这一点?还是这才是真正的问题?
【问题讨论】:
-
嘿,我也在做一个 Swift YouTube 项目。我在使用刷新令牌获取 Alamofire 的常规令牌时遇到问题,您有什么可以提供帮助的机会吗?错误 = "unsupported_grant_type",描述为 "Invalid grant_type:"。谢谢!
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) -
当然。将问题和控制器的格式化源代码作为问题发布并在 cmets 中标记我。很难知道该回答什么,因为我只看到您的代码的一小部分。
标签: ios swift oauth-2.0 youtube-data-api