2017-07-06 17 views
1

私はYouTube APIを使用して動画をアップロードする必要があるiOSモバイルアプリケーションを開発しています。一般的に、私は必要なワークフローを理解しています:Youtube APIのOAuth2アクセストークンをSwiftから取得する

1)clientId、clientSecret、およびその他の詳細を使用して認証エンドポイントに要求を送信します。

2)サービスはクライアントを認証し、アクセストークンを含むクライアント指定のcallbackURLに要求を返します。

3)クライアントは、将来のリクエストを送信したいときはいつでも、ヘッダーにトークンを提供します。

私はnode.jsスクリプトを使用してYoutubeビデオを正常にアップロードしましたが、これをSwiftで実装する方法を理解するのに苦労しています。私はのviewDidLoad()コントローラの段階で私のアクセストークンを取得しようとしています

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) 
     } 

:私のVideoManagementControllerでは、私はsample.mp4ファイルのアップロードをトリガーするボタンがあります。

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 
     } 
    }) 

問題は、アクセストークンの取得コードが本質的にハングアップして完了しないことです。これは、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 

     } 
     else { 
      print("TOKEN: Authorization error: \(String(describing: error?.localizedDescription))") 
      self.authState = nil 
     } 

私はどちらかの新しいトークンを取得したり、authorization errorプリントアウト得ることはありません。

私はcallbackURLと関係があると思われます。スウィフトは自分のアクセストークンを取得する場所を "聴く"ことを知っているとは思わない。たとえば、node.jsにはサーバー側を設定するのが比較的簡単ですが、Swiftではどのようにしたらいいですか?それとも本当の問題なのでしょうか?

+0

を通じて使用して、私を助け、それが役立つことを願ってSwift YouTubeプロジェクトにも取り組んでいます。 Alamofireで定期的なトークンを取得するためにリフレッシュトークンを使用する際に問題が発生しました。お手伝いできますか? error = "unsupported_grant_type"、記述は "Invalid grant_type:"です。ありがとう! 'let endpoint =" https://www.googleapis.com/oauth2/v4/token ";パラメータ= ["client_id":クライアント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 
関連する問題