2017-03-07 13 views
2

我目前正在嘗試使用outlook郵件服務來獲取聯繫人/約會和郵件。但是我偶然發現了一個問題。展望OAuth2不能重定向到應用程序

我使用OAuth2Swift作爲庫,使我所有的OAuth調用,因爲我正在集成多個服務。

我創建了一個URL scheme像他們README enter image description here

然後我創建了一個Constants文件,它看起來像這樣

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) 

我創建的Outlook應用程序上https://apps.dev.microsoft.com
生成我的鑰匙和描述祕密並將其填充到我的應用程序中。

我將mobile application平臺添加到我的應用程序。它告訴我使用重定向URI urn:ietf:wg:oauth:2.0:oob

所以我的代碼授權看起來像這樣

@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)") 
      } 
      ) 
     } 
當我運行代碼,我第一次看到一個屏幕,進入我的郵箱/密碼

。當我輸入我的郵件時,它會將我重定向到一個不同的頁面/門戶,我可以輸入我的密碼。當我輸入密碼時,它會顯示權限屏幕。

enter image description here

,只要我打yes它會給我一個錯誤說「由於地址無效,Safari無法打開網頁」。我很確定這與redirect URI有關,但我不知道該如何解決這個問題。

我希望有人能夠幫助我這個!

回答

0

我想你忘記了你的URL方案設置到指定的標識符urn:ietf:wg:oauth:2.0:oob(見第一形象:沒有任何標識集)

+0

我加入,但試圖與Outlook登錄時,它沒有任何區別了同樣的信息顯示出來 – NoSixties

0

我想你忘了處理redirect URI在AppDelegate中從OAuthSwift庫!你必須在AppDelegate中處理回調,如下所示。

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

忘了補充一點,但我有我的委託,它的方法和它甚至不被調用。它確實被調用,當我用twitter做它 – NoSixties

+0

嗨,你能解決這個問題嗎?我嘗試使用Xamarin-Mac-app對OneDrive進行身份驗證時遇到了同樣的問題。 – Tom

+0

這裏已經提到 - https://github.com/OAuthSwift/OAuthSwift – KiranJasvanee

相關問題