Azure-Samples / ms-identity-mobile-apple-swift-objc

An iOS sample in Swift that authenticates Microsoft Account and Azure AD users and calls the Graph API using OAuth 2.0
https://aka.ms/aaddev
74 stars 37 forks source link

Not getting refreshToken #93

Closed alpha-sagar closed 3 days ago

alpha-sagar commented 4 months ago

I'm using like this :

import MSAL

class ViewController: UIViewController {
    let clientID = "your_client_id"
    let redirectUri = "your_redirect_uri"
    let authority = "https://login.microsoftonline.com/common"

    override func viewDidLoad() {
        super.viewDidLoad()

        do {
            let msalConfig = try MSALPublicClientApplicationConfig(clientId: clientID, redirectUri: redirectUri, authority: MSALAuthority(url: URL(string: authority)!))
            let applicationContext = try MSALPublicClientApplication(configuration: msalConfig)

            let webViewParameters = MSALWebviewParameters(parentViewController: self)
            let parameters = MSALInteractiveTokenParameters(scopes: ["User.Read"], webviewParameters: webViewParameters)

            applicationContext.acquireToken(with: parameters) { (result, error) in
                if let error = error {
                    print("Could not acquire token: \(error.localizedDescription)")
                    return
                }

                guard let authResult = result else {
                    print("Auth result is nil")
                    return
                }

                print("Access token is \(authResult.accessToken ?? "No access token")")
                print("Refresh token is \(authResult.refreshToken ?? "No refresh token")")

                // Log all properties of the auth result for debugging
                print("Authorization response: \(authResult.authorizationHeader ?? "No authorization header")")
                print("ID Token: \(authResult.idToken ?? "No ID token")")
                print("Scopes: \(authResult.scopes)")
                print("Expires on: \(authResult.expiresOn ?? Date())")
            }
        } catch {
            print("Error creating MSALPublicClientApplication: \(error)")
        }

        MSALGlobalConfig.loggerConfig.setLogCallback { (logLevel, message, containsPII) in
            if !containsPII {
                print("%@", message ?? "No message")
            }
        }
    }
}

I need authResult.refreshToken which I need to be give to server to fetch all Calendar related data without the help of client all the time but I'm unable to fetch the refresh token

Scopes Given : "User.Read" , "Calendars.ReadWrite" & "Offline_access" On Azure Portal

Please help to fix this one ☝️

mipetriu commented 3 months ago

Hello @alpha-sagar, looking at your provided sample, I'm not seeing a silent token request. In order to use the refresh token for a silent authentication, you should first call acquireTokenSilent.

See the implementation in this sample as a guide. First check if a silent token acquisition is possible, and then acquire interactively if needed.

mipetriu commented 3 days ago

Hello @alpha-sagar, I will close this issue due to inactivity. Please feel free to re-open if more help is needed.