title: Getting OAuth2 access token for Youtube API through Swift
body: <p>I am working on an iOS mobile application where I need to upload videos using the Youtube Data API. In general, I understand the workflow needed:</p>
  
  <p>1) Send a request to an authentication endpoint using clientId, clientSecret, and other details.</p>
  
  <p>2) The service authenticates the client, and sends back a request to a client-specified callbackURL containing the access token. </p>
  
  <p>3) The client provides the token in the header whenever he/she wants sends future requests.</p>
  
  <p>I've successfully uploaded Youtube videos using node.js script, but I'm having a lot of trouble understanding how to implement this in Swift. In my <strong>VideoManagementController</strong>, I have a button that triggers the upload of a <strong>sample.mp4</strong> file:</p>
  
  <pre><code>   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)
          }
  </code></pre>
  
  <p>I am attempting to retrieve my access token in the <strong>viewDidLoad()</strong> stage of the controller:</p>
  
  <pre><code>  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?) -&gt; 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
          }
      })
  </code></pre>
  
  <p>The issue is that my access token retrieval code essentially hangs and never completes. It reaches the print statement <code>"REACHED"</code> but never comes out of this following portion of code:</p>
  
  <pre><code>appDelegate?.currentAuthorizationFlow = OIDAuthState.authState(byPresenting: request, presenting: self, callback: {(_ authState: OIDAuthState?, _ error: Error?) -&gt; 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
          }
  </code></pre>
  
  <p>I do not either get a new token or get the <code>authorization error</code> print out.</p>
  
  <p>I suspect it has something to do with my callbackURL. I don't think Swift knows where to "listen" to get my access token from. A callbackURL endpoint is relatively easy to set up server-side in node.js, for example, but how do I do this in Swift? Or is this even the real issue?</p>