[GH-ISSUE #348] OAuth with Outlook fails as soon as you give permission #219

Closed
opened 2026-03-03 16:46:46 +03:00 by kerem · 4 comments
Owner

Originally created by @MaikoHermans on GitHub (Mar 8, 2017).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/348

Description:

I'm currently trying use the outlook mail service to obtain contacts/appointments and mails. However I've stumbled upon a problem.

I created a URL scheme like described in their README

README
Then I created a Constants file which looks like this

struct Consumer {
    let consumerKey: String
    let consumerSecret: String
    let authorizeURL: String
    let accessTokenURL: String
    let responseType: String?
    let requestTokenURL: String?
}

let Outlook = Consumer(
    consumerKey: "",
    consumerSecret: "",
    authorizeURL: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
    accessTokenURL: "https://login.microsoftonline.com/common/oauth2/v2.0/token",
    responseType: "code",
    requestTokenURL: nil)

I created an application for outlook on https://apps.dev.microsoft.com
generated my key and secret and filled them in inside my application.

I added the mobile application platform to my app. Which tells me to use the redirect URI urn:ietf:wg:oauth:2.0:oob

So my code to authorise looks like this

@IBAction func btn_Outlook(_ sender: Any) {
        let oauthOU = OAuth2Swift(
            consumerKey: Outlook.consumerKey,
            consumerSecret: Outlook.consumerSecret,
            authorizeUrl: Outlook.authorizeURL,
            accessTokenUrl: Outlook.accessTokenURL,
            responseType: Outlook.responseType!)

        oauthOU.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthOU)
        oauthOU.authorize(
            withCallbackURL: "urn:ietf:wg:oauth:2.0:oob",
            scope: "https://outlook.office.com/Mail.ReadWrite https://outlook.office.com/Mail.Send https://outlook.office.com/Calendars.ReadWrite https://outlook.office.com/Contacts.ReadWrite https://outlook.office.com/Tasks.ReadWrite",
            state: state,
            success: { credential, response, parameters in
                print("logged in with \(credential), with response \(response) and parameters \(parameters)")},
            failure: { error in
                print("error occured \(error.localizedDescription)")
            }
            )
        }

when I run the code I first get a screen to enter my mail/password. When I enter my mail it will redirect me to a different page/portal where I can enter my password. When I've entered my password it will show me the permissions screen.

PERMISSION SCREEN

as soon as I hit yes it will give me an error saying "Safari cannot open the page because the address is invalid." I'm pretty sure this has to do with the redirect URI but I'm not sure what to do to actually fix this.

Could you possibly tell me where I might be going wrong with this?

EDIT:
I added urn:ietf:wg:oauth:2.0:oob to my url scheme as well but this didn't change anything.

I've also added

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
  if (url.host == "oauth-callback") {
    OAuthSwift.handle(url: url)
  }
  return true
}

but it doesn't even get here.

OAuth Provider (Outlook):

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

  • iOS :
  • OSX :
  • TVOS :
  • WatchOS :

Installation method:

  • Carthage
  • CocoaPods
  • Manually

Library version:

  • head
  • v1.0.0
  • v0.6
  • other: (Please fill in the version you are using.)

Xcode version:

  • 8.0 (Swift 3.0)

  • 8.0 (Swift 2.3)

  • 7.3.1

  • other: (Please fill in the version you are using.)

  • objective c

Originally created by @MaikoHermans on GitHub (Mar 8, 2017). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/348 ### Description: I'm currently trying use the outlook mail service to obtain contacts/appointments and mails. However I've stumbled upon a problem. I created a `URL scheme` like described in their `README` ![README](https://i.stack.imgur.com/mBqsB.png) Then I created a Constants file which looks like this ``` swift struct Consumer { let consumerKey: String let consumerSecret: String let authorizeURL: String let accessTokenURL: String let responseType: String? let requestTokenURL: String? } let Outlook = Consumer( consumerKey: "", consumerSecret: "", authorizeURL: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize", accessTokenURL: "https://login.microsoftonline.com/common/oauth2/v2.0/token", responseType: "code", requestTokenURL: nil) ``` I created an application for outlook on https://apps.dev.microsoft.com generated my `key` and `secret` and filled them in inside my application. I added the `mobile application` platform to my app. Which tells me to use the `redirect URI urn:ietf:wg:oauth:2.0:oob` So my code to authorise looks like this ``` swift @IBAction func btn_Outlook(_ sender: Any) { let oauthOU = OAuth2Swift( consumerKey: Outlook.consumerKey, consumerSecret: Outlook.consumerSecret, authorizeUrl: Outlook.authorizeURL, accessTokenUrl: Outlook.accessTokenURL, responseType: Outlook.responseType!) oauthOU.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthOU) oauthOU.authorize( withCallbackURL: "urn:ietf:wg:oauth:2.0:oob", scope: "https://outlook.office.com/Mail.ReadWrite https://outlook.office.com/Mail.Send https://outlook.office.com/Calendars.ReadWrite https://outlook.office.com/Contacts.ReadWrite https://outlook.office.com/Tasks.ReadWrite", state: state, success: { credential, response, parameters in print("logged in with \(credential), with response \(response) and parameters \(parameters)")}, failure: { error in print("error occured \(error.localizedDescription)") } ) } ``` when I run the code I first get a screen to enter my mail/password. When I enter my mail it will redirect me to a different page/portal where I can enter my password. When I've entered my password it will show me the permissions screen. ![PERMISSION SCREEN](https://i.stack.imgur.com/aF9Z4.png) as soon as I hit `yes` it will give me an error saying "Safari cannot open the page because the address is invalid." I'm pretty sure this has to do with the `redirect URI` but I'm not sure what to do to actually fix this. Could you possibly tell me where I might be going wrong with this? **EDIT:** I added `urn:ietf:wg:oauth:2.0:oob` to my url scheme as well but this didn't change anything. I've also added ``` swift func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { if (url.host == "oauth-callback") { OAuthSwift.handle(url: url) } return true } ``` but it doesn't even get here. ### OAuth Provider (Outlook): ### OAuth Version: - [ ] Version 1 - [x] Version 2 ### OS (Please fill the version) : - [x] iOS : - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [ ] Carthage - [x] CocoaPods - [ ] Manually ### Library version: - [ ] head - [x] v1.0.0 - [ ] v0.6 - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [x] 8.0 (Swift 3.0) - [ ] 8.0 (Swift 2.3) - [ ] 7.3.1 - [ ] other: (Please fill in the version you are using.) - [ ] objective c
kerem 2026-03-03 16:46:46 +03:00
Author
Owner

@phimage commented on GitHub (Mar 8, 2017):

urn:ietf:wg:oauth:2.0:oob is not a valid url scheme and cannot be open by safari
there is no ://

if you create the url scheme like readme say, use it.. oauthTestAll:// in your callbackurl
and if microsoft don't accept it, you must do something else (go to wiki, maybe https://github.com/OAuthSwift/OAuthSwift/wiki/API-with-only-HTTP-scheme-into-callback-URL could help )

<!-- gh-comment-id:285189873 --> @phimage commented on GitHub (Mar 8, 2017): urn:ietf:wg:oauth:2.0:oob is not a valid url scheme and cannot be open by safari there is no `://` if you create the url scheme like readme say, use it.. oauthTestAll:// in your callbackurl and if microsoft don't accept it, you must do something else (go to wiki, maybe https://github.com/OAuthSwift/OAuthSwift/wiki/API-with-only-HTTP-scheme-into-callback-URL could help )
Author
Owner

@MaikoHermans commented on GitHub (Mar 9, 2017):

@phimage
microsoft only accepts urn:ietf:wg:oauth:2.0:oob that's a value they give you and you can't change.
I tried to use the http route but the thing is that outlook redirects me to the specified url instantly when it opens up the login page.

I think outlook redirects to the correct login page based on your mail address. When it tries to redirect it will automatically open the redirect url. I'm not sure how to fix this.

<!-- gh-comment-id:285308632 --> @MaikoHermans commented on GitHub (Mar 9, 2017): @phimage microsoft only accepts `urn:ietf:wg:oauth:2.0:oob` that's a value they give you and you can't change. I tried to use the http route but the thing is that outlook redirects me to the specified url instantly when it opens up the login page. I think outlook redirects to the correct login page based on your mail address. When it tries to redirect it will automatically open the redirect url. I'm not sure how to fix this.
Author
Owner

@phimage commented on GitHub (Mar 9, 2017):

maybe you choose native app or mobile app instead of web app, that's why you can change the redirect uri (some other providers as some configuration like that)

for urn:ietf:wg:oauth:2.0:oob, in my previous post I put a link, maybe implementing web view delegate allow to manage this redirect uri, and call OAuthSwift.handle

please provide a link to the documentation, maybe some one can help

<!-- gh-comment-id:285314392 --> @phimage commented on GitHub (Mar 9, 2017): maybe you choose native app or mobile app instead of web app, that's why you can change the redirect uri (some other providers as some configuration like that) for urn:ietf:wg:oauth:2.0:oob, in my previous post I put a link, maybe implementing web view delegate allow to manage this redirect uri, and call OAuthSwift.handle please provide a link to the documentation, maybe some one can help
Author
Owner

@phimage commented on GitHub (Jun 4, 2017):

I add one comment on this issue
let oauthOU = OAuth2Swift -> retain issue, the object oauthOU must be set as class var

<!-- gh-comment-id:306030547 --> @phimage commented on GitHub (Jun 4, 2017): I add one comment on this issue let oauthOU = OAuth2Swift -> retain issue, the object oauthOU must be set as class var
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#219
No description provided.