OGeek|极客世界-中国程序员成长平台

标题: ios - Outlook OAuth2 无法重定向到应用程序 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:09
标题: ios - Outlook OAuth2 无法重定向到应用程序

我目前正在尝试使用 Outlook 邮件服务来获取联系人/约会和邮件。但是我偶然发现了一个问题。

我正在使用 OAuth2Swift因为我正在集成多个服务,所以作为库来进行我所有的 OAuth 调用。

我创建了一个 URL 方案,如他们的 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)

我在 https://apps.dev.microsoft.com 上创建了一个 Outlook 应用程序
生成我的 key 和 secret 并将它们填写在我的应用程序中。

我将 移动应用程序 平台添加到我的应用程序中。这告诉我使用重定向URI urn:ietf:wgauth:2.0ob

所以我的授权代码如下所示

@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:wgauth:2.0ob",
            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 有关,但我不确定如何才能真正解决这个问题。

我希望有人能够帮助我解决这个问题!



Best Answer-推荐答案


我认为您忘记在 URL 方案设置中指定标识符 urn:ietf:wgauth:2.0ob(见第一张图片:没有设置标识符)

关于ios - Outlook OAuth2 无法重定向到应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42644818/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4