Forum Discussion
How to authenticate to SharePoint Online Site from Office scripts?
Hi Folks,
I'm referring below document to run an office script for adding images to Excel Table:
https://learn.microsoft.com/en-us/office/dev/scripts/resources/samples/add-image-to-workbook
The image I want to add is stored in a SharePoint Library. The link to the images are available in each rows of the table. I'm able to get the URL, how can I fetch the image from that URL. I'm stuck at the authentication stage. What is best approach to connect to SPO via office scripts as I know it does not support native login popups?
Thanks
2 Replies
- LeonPavesicSilver Contributor
Hi rbn_neu,
To authenticate to a SharePoint Online Site from Office scripts, you can use one of the following approaches:
- Use the Microsoft Graph API
The Microsoft Graph API is a REST API that provides access to a wide range of Microsoft 365 data, including SharePoint Online. To use the Microsoft Graph API to authenticate to a SharePoint Online site, you will need to get an OAuth 2.0 access token.
To get an OAuth 2.0 access token, you will need to register an app in Azure Active Directory (AD) and grant the app the necessary permissions to access the SharePoint Online site. Once you have registered an app and granted it the necessary permissions, you can use the following code to get an OAuth 2.0 access token:// Get the app's client ID and client secret const clientId = "YOUR_CLIENT_ID"; const clientSecret = "YOUR_CLIENT_SECRET"; // Get the OAuth 2.0 access token const accessToken = await fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: `client_id=${clientId}&client_secret=${clientSecret}&scope=https://graph.microsoft.com/.default` }); // Convert the access token response to JSON const accessTokenResponse = await accessToken.json(); // Get the access token const accessTokenValue = accessTokenResponse.access_token;
Once you have an OAuth 2.0 access token, you can use it to authenticate to the Microsoft Graph API and access the SharePoint Online site. To access the SharePoint Online site, you will need to make a request to the following endpoint:
https://graph.microsoft.com/v1.0/sites/{siteId}
Replace {siteId} with the ID of the SharePoint Online site you want to access.
- Use the SharePoint Online REST API
The SharePoint Online REST API is a REST API that provides access to SharePoint Online data. To use the SharePoint Online REST API to authenticate to a SharePoint Online site, you will need to get a SharePoint Online access token.
To get a SharePoint Online access token, you will need to register an app in Azure Active Directory (AD) and grant the app the necessary permissions to access the SharePoint Online site. Once you have registered an app and granted it the necessary permissions, you can use the following code to get a SharePoint Online access token:
// Get the app's client ID and client secret const clientId = "YOUR_CLIENT_ID"; const clientSecret = "YOUR_CLIENT_SECRET"; // Get the SharePoint Online access token const accessToken = await fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: `client_id=${clientId}&client_secret=${clientSecret}&scope=https://graph.microsoft.com/.default` }); // Convert the access token response to JSON const accessTokenResponse = await accessToken.json(); // Get the access token const accessTokenValue = accessTokenResponse.access_token;
Once you have a SharePoint Online access token, you can use it to authenticate to the SharePoint Online REST API and access the SharePoint Online site. To access the SharePoint Online site, you will need to make a request to the following endpoint:
https://{tenant}.sharepoint.com/_api/web/GetByServerRelativeUrl('{siteUrl}')
Replace {tenant} with the name of your SharePoint Online tenant and {siteUrl} with the URL of the SharePoint Online site you want to access.
If you need to access SharePoint Online data from other Microsoft 365 services, such as Microsoft Teams or Microsoft OneDrive, then you should use the Microsoft Graph API.
If you need to access SharePoint Online data from outside of Microsoft 365, then you should use the SharePoint Online REST API.- Microsoft Graph API documentation: https://learn.microsoft.com/en-us/graph/
- SharePoint Online REST API documentation: https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/get-to-know-the-sharepoint-rest-service
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)- mgullonCopper ContributorThe two code examples to get an OAuth 2.0 access token and a SharePoint Online access token are exactly same. Is there an error here?