Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not getting refreshToken #93

Open
alpha-sagar opened this issue Jun 11, 2024 · 1 comment
Open

Not getting refreshToken #93

alpha-sagar opened this issue Jun 11, 2024 · 1 comment
Assignees
Labels

Comments

@alpha-sagar
Copy link

alpha-sagar commented Jun 11, 2024

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
Copy link
Contributor

mipetriu commented Aug 5, 2024

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 mipetriu self-assigned this Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants