Reputation: 5927
I'm trying to access the sharepoint Excel charts I have used the following query in Microsoft graph explorer
https://graph.microsoft.com/v1.0/sites/yyyyyy.com/drives/folderiddd/items/id2222/workbook/worksheets('Sheet1')/charts('Chart 2')/Image(width=300,height=300,fittingMode='fit')
This is giving the result.
From the inspect element , network tab I got the Authorization
and I made the Jquery ajax to get the content from sharepoint
var fileCollectionEndpoint = "https://graph.microsoft.com/v1.0/sites/yyyyyy.com/drives/folderiddd/items/id2222/workbook/worksheets('Sheet1')/charts('Chart 2')/Image(width=300,height=300,fittingMode='fit')";
$.ajax({
url: fileCollectionEndpoint,
async: false,
dataType: 'json',
type: "GET",
headers: {
'Authorization':'This is copied from the Microsoft graph explorer',
},
success: function (json) {
var imgsource ="data:image/png;base64,"+json.value;
$("body").append("<img id='ddd' src='"+imgsource+"' />");
}
});
The above code is working fine. But the problem is every one hour once this token getting expired I'm trying to generate the token using rest-api because everytime I can't copy paste from the Microsoft Graph Explorer. How can I do it?
Upvotes: 2
Views: 6179
Reputation: 2447
The Graph explorer is great for testing API calls but like you discovered access tokens expire after an hour so you'll need to use our token endpoint to get tokens for your app. There's an excellent guide for this at Get access tokens to call Microsoft Graph. In summary, register your app at apps.dev.microsoft.com and then decide whether your app will let users authenticate or if your app will run in a server/daemon context without user authentication.
I'm not certain from your question but if you're developing in SharePoint there's another option so users don't have to login to your app inside of SharePoint.
There's a SharePoint documentation page for using GraphHttpClient to call Microsoft Graph . It uses the Yeoman SharePoint generator which has a setup guide at Scaffold projects by using Yeoman SharePoint generator.
Steps:
Install the npm package
npm install @microsoft/generator-sharepoint -g
Create a new web part by running the Yeoman SharePoint generator.
mkdir hellograph-webpart
cd hellograph-webpart
yo @microsoft/sharepoint
After it's setup, you can import a GraphHttpClient to make REST calls to Microsoft Graph.
import { GraphHttpClient, HttpClientResponse, IGraphHttpClientOptions } from '@microsoft/sp-http';
protected _readGroups(){
this.context.graphHttpClient.get(`v1.0/sites/yyyyyy.com/drives/folderiddd/items/id2222/workbook/worksheets('Sheet1')/charts('Chart2')/Image(width=300,height=300,fittingMode='fit')`, GraphHttpClient.configurations.v1).then((response: HttpClientResponse) => {
if (response.ok) {
return response.json();
} else {
console.warn(response.statusText);
}
}).then((result: any) => {
// Transfer result values to the group variable
this._renderTable(result.value);
});
}
Upvotes: 2
Reputation: 932
You need to request access token from Microsoft graph for the SharePoint resource you are trying to access.
You can use ADAL Js to generate access token for Sharepoint resource using OAuth.
Upvotes: 0