-
Notifications
You must be signed in to change notification settings - Fork 804
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
How can I get access token for multiple resources based on sample Office-Add-in-Microsoft-Graph-React #588
Comments
@tiwarigaurav Please ask this question on on https://stackoverflow.com/. Tag your question with office-js or outlook-web-addins This repo is only for questions related to its samples. |
@Rick-Kirkham - not sure why this was closed. My question specifically relates to the sample "Office-Add-in-Microsoft-Graph-React" Using this sample I can get the Graph Access Token just fine and everything works. My Question is - How can I get access token for multiple resources using the msalInstance.acquireTokenSilent(scope) within the office-apis-helpers.ts file? The main idea is to use the login popup once to initiate the msalinstance and for any subsequent scopes/resources (or when the initial access token expires) use the acquireTokenSilent method - so that the user does not need to click the login button again to get another token. |
@davidchesnut Please let me know if you need more details in investigating our issue. We anticipate quick response and resolution to above. |
@tiwarigaurav I leave it to @davidchesnut to decide, but I don't think this "relates" to the sample in the way that is intended. The sample doesn't have a bug and your question is not about the sample as it is. You are asking for help for a distinctly different scenario. You really should ask this question on Stack Overflow. |
@Rick-Kirkham - what I am trying to imply is that the sample provided does not work in real-world scenarios, neither can we make it work in real world. As per Microsoft documentation the -
Which means (my interpretation) is that we cannot use msalInstance.acquireTokenSilent(scope). And that every time we need to get a new access token we will need the user to click the login button. With the above statement I can safely say that the sample provided under OfficeDev -> Office-Add-in-samples -> auth -> Office-Add-in-Microsoft-Graph-React cannot be used in real world (or the concept cannot be reused in real world). Unless I am missing something quite obvious which is why I have posted this question. |
@davidchesnut - any updates for us on this issue? |
Hi @tiwarigaurav, It appears you are looking for information on how to use MSAL in react to get additional scopes beyond the one used in this sample. This sample in the Azure Active Directory documentation may be better aligned to help with your question. Specifically, the scopes are what you pass in to specify what API/scope of access you want granted in the token, which you can find in the 'Acquire token for an API' part of that sample. If you are still having issues with MSAL and accessing resources using it, the best spot to ask would be Stack Overflow using msal and msal.js tags. Closing the issue on this sample as your question is about MSAL usage - if you find issues with MSAL.js you can more information on their GitHub page/open an issue there. |
@mattgeim - It is possibly my fault - the title of the question is a bit misleading. Let's take out the multiple scopes part out of the equation and concentrate on this sample - OfficeDev -> Office-Add-in-samples -> auth -> Office-Add-in-Microsoft-Graph-React I am trying to imply is that the sample provided does not work in real-world scenarios, neither can we make it work in real world. i.e. every time we need to get a new access token we will need the user to click the login button and show the dialogue popup. Perhaps this should be raised as a bug in the sample? Note: from what I have gathered so far is that "Office Add-In + React + MSAL.JS + Graph API" will not work cross platform and cross browsers. |
Thanks for the response clarifying @tiwarigaurav. This sample doesn't do SSO, its MSAL based and isn't trying to reduce sign-ins. If you are interested in SPA flows for SSO, you can read more about it here: https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-js-sso please note this is not related to add-ins, but rather SSO flows and MSAL. This sample works for what it should do, but I will happily take your feedback and discuss with our team on ways to improve on clarity for others in the future - thank you for the feedback! |
@mattgeim - this isn't about SSO with Office Add-Ins. My feedback is that this sample Office-Add-in-Microsoft-Graph-React will NOT work in real world. There is a bug with Office Add-Ins Dialogue API I found this on MS documentation. This sample should clearly state this in on the wiki page. |
Hi @tiwarigaurav, If you want to use MSAL caching so that you don't have to pop up a dialog for every API call, see getAccessTokenMSAL in the Office-Add-in-NodeJS-SSO sample. You can also see how to send the home account ID from the dialog box in the handleResponse method. This is all MSAL code based on the Microsoft identity platform, and nothing specific to Office JS. In the comments for authConfig.js and authRedirect.js there are links to the original MSAL samples those are based on. However it is possible there are some Office platform/webview combinations that might not work with this MSAL caching mechanism (in which case it defaults back to popping up the auth dialog.) I can look into updating the React sample down the road with the caching code. Are there any other issues you spotted in the sample? Thanks! |
@davidchesnut - thanks for responding. I tried this and it does NOT work. I tried your suggestion with the below code (in my app's login.ts file):
With the above I was able to pass the homeAccountId from the Dialog box to the app but msalInstance.getAccountByHomeId(messageFromDialog.result.account.homeAccountId) returns null. As per my research there is a bug with the Office Dialog API in which - the web add-in's Task Pane and Dialog Box do not share the same Local Storage. So, when we initialize a new msal instance in the app it has nothing in common with the msal instance from the Dialog box so the getAccountByHomeId will fail. Note: I am testing this on Outlook desktop app running on Windows |
Hi @tiwarigaurav, I did some more research into this. The Microsoft Graph React auth sample solves this problem by storing the token in a variable. See line 51 in App.tsx. I checked with the team, and in the context of SPA using implicit grant with PKCE, this is fine. This code pattern allows you to reuse the token on subsequent calls without needing to sign in again. I noticed the sample had some out of date libraries and a minor typeScript issue. I fixed these in PR #605. So you should be able to use this sample once the PR goes through. Hope this helps, |
Hi @davidchesnut, Thanks for taking the time to update this. I might be missing something here OR I have been unable to explain the issue - please bare with me. Getting the token back from the "displayDialogAsync" popup saving it in a variable in the App.tsx file and using it to access GraphAPI has been fine and is working as expected. The issue that I am trying to get help on is:
Trying to use the acquireTokenSilent from the REACT app fails with error: |
Hi @tiwarigaurav, you are correct, and the token expires. I've been looking into this for any possible workarounds but it doesn't look like this is possible and you have to sign the user in again. This is definitely a pain point and I'll share this with the rest of the product team to look into. The one approach that will work is using SSO. Office will refresh the token for you automatically if you request it through Hope this helps, |
@davidchesnut - thanks for acknowledging that there is an issue. I was thinking that I was going out of my mind ;) SSO does not help - if we need to create cross browser/cross platform Add-Ins we need a fall back method (SSO will not work in Safari and other exceptions). The only conclusion that I have reached is - msal-browser/msal-react does not work with Office web add-ins in real world scenarios and because of this, even msal-node OR msal .net will not work. If we need to implement a real world add-in we need to use OBO flow and manage access and refresh tokens ourselves i.e. using a DB. |
Question
Ask your question here.
I am developing an Outlook Web Add-In using
I followed the example from "Office-Add-in-Microsoft-Graph-React" and I am able to successfully get the access token for Graph API.
My question is how can I request access token for multiple resources using the msalInstance i.e. SharePoint, Onedrive.
The text was updated successfully, but these errors were encountered: