2017-07-06 171 views
1

我正在使用Youtube Data API上传视频的iOS移动应用程序。一般来说,我了解所需的工作流程:通过Swift获取Youtube API的OAuth2访问令牌

1)使用clientId,clientSecret和其他详细信息向身份验证端点发送请求。

2)该服务对客户端进行身份验证,并将请求发送回客户端指定的包含访问令牌的callbackURL。

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打印出来。

我怀疑它与我的callbackURL有关。我不认为Swift知道从哪里“听”来获取我的访问令牌。例如,callbackURL端点在node.js中设置服务器端相对容易,但我如何在Swift中执行此操作?或者这甚至是真正的问题?

+0

嘿,我也在制作一个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(端点,方法:.post,参数:参数,编码:JSONEncoding.default)' –

+0

当然。将控制器的问题和格式化的源代码作为问题发布,并在评论中加上标签。由于我只看到一小段代码,因此很难知道该回答什么。 –

+0

https://stackoverflow.com/questions/44989836/swift-alamofire-getting-token-from-refresh-token-request-error –

回答

0

我已经厌倦了在我的代码像这样,你还需要检查的某个编码类型URLEncoding.httpBody希望对大家有所帮助,它帮助我在使用OAuth的通过

let headers = [ 
     "Content-Type": "application/x-www-form-urlencoded" 
    ] 

    Alamofire.request("https://accounts.google.com/o/oauth2/revoke?token={\(token)}", method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: headers).responseJSON { (response:DataResponse<Any>) in 
相关问题