title: Using SFAuthenticationSession to log in user to Microsoft Graph
body: <p>I've been using <a href="https://github.com/p2/OAuth2" rel="nofollow noreferrer">p2-oauth2</a> library earlier to be able to log in through a <code>safariViewController</code>, but since the latest iOS version (11.3) I found out that my app were crashing all the time when the user tries to log in. I didn't get any error messages, but after a while I found out that <code>SFAuthenticationSessions</code> is the way to go when using SSO (single sign on).</p>
  
  <p>My old code were pretty much like this (Using p2_oauth2):</p>
  
  <pre><code>static var oauth2 = OAuth2CodeGrant(settings: [
      "client_id": "myClientID",
      "authorize_uri": "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
      "token_uri": "https://login.microsoftonline.com/common/oauth2/v2.0/token",
      "scope": "User.Read Mail.Read Calendars.ReadWrite Calendars.Read Calendars.Read.Shared Offline_access",
      "redirect_uris": ["myRedirectURI"],
      "keychain": true
      ])
  
  func loginToOffice365(completion: @escaping (_ error: Error? ) -&gt; ()) {
      var userDataRequest: URLRequest {
          var request = URLRequest(url: URL(string: "https://graph.microsoft.com/v1.0/me/")!)
          request.setValue("Bearer \(OauthManager.oauth2.accessToken ?? "")", forHTTPHeaderField: "Authorization")
          return request
      }
  
      alamofireManager.request(userDataRequest).validate().responseJSON { 
   (response) in
      switch response.result {
      case .success( _):
      //Handle user information
          completion(nil)
      case .failure(let error):
          completion(error)
              }
          }
  }
  </code></pre>
  
  <p>I tried to implement in <code>SFAuthenticationSession</code> in my project, and it was requiring a URL as a parameter. So I have been searching for a while for a Microsoft URL to make it possible to send in clientId, scope, and redirectURI in the same URL. And here's the result so far:</p>
  
  <pre><code>let url = URL(string: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?state=78E99F6B&amp;response_type=code&amp;scope=User.Read+Mail.Read+Calendars.ReadWrite+Calendars.Read+Calendars.Read.Shared&amp;redirect_uri=MYREDIRECTURI&amp;client_id=MYCLIENTID")!
          OauthManager.authenticationSession = SFAuthenticationSession(url: url, callbackURLScheme: nil, completionHandler: { (successUrl: URL?, error: Error?) in
              if let error = error {
                  print(error)
                  completion(error)
              } else {
                  var accessToken = ""
                  if let absolutString = successUrl?.absoluteString, let urlComponents = URLComponents(string: absolutString)?.query {
                      accessToken = urlComponents
                  }
                  print(accessToken)
                  completion(nil)
              }
          })
          OauthManager.authenticationSession?.start()
  </code></pre>
  
  <p>So now I finally received an access token from Microsoft. But where should I go from here? How do I get refresh tokens, make it possible to start calling Microsoft graph API calls?</p>
  
  <p>If you know any better solution or any advice I'll be glad to receive them! This is my first project using login, because I'm fairly new to Swift.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I can also mention that Microsoft documentation recommends using these libraries:</p>
  
  <ul>
  <li>Microsoft Authentication Library (MSAL) client libraries are
  available for .NET, JavaScript, Android, and Objective-C. All
  platforms are in production-supported preview, and, in the event
  breaking changes are introduced, Microsoft guarantees a path to
  upgrade.</li>
  <li>Server middleware from Microsoft is available for .NET Core and
  ASP.NET (OWIN OpenID Connect and OAuth) and for Node.js (Microsoft
  Azure AD Passport.js).</li>
  <li>The v2.0 endpoint is compatible with many third-party authentication
  libraries.</li>
  </ul>
  
  <p><a href="https://developer.microsoft.com/en-us/graph/docs/concepts/auth_overview" rel="nofollow noreferrer">https://developer.microsoft.com/en-us/graph/docs/concepts/auth_overview</a></p>
  
  <p>I've tried MSAL and AppAuth, but they just didn't gave me any response back.</p>