2017-07-06 123 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