Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to access Microsoft graph from SharePoint online aspx pages?

$
0
0

I have SharePoint 2013 online site and from one of the aspx page I want to access some of the Microsoft Graph endpoints.

I am using following code in $(document).ready to get the access token to connect to Graph:

function getGraphAccessToken()
{
	var deferred = new jQuery.Deferred();
	var requestHeaders = {
            'X-RequestDigest': $("#__REQUESTDIGEST").val(),"accept": "application/json;odata=nometadata","content-type": "application/json;odata=nometadata"
        };
	var resourceData = {"resource": "https://graph.microsoft.com",
        };

	$.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.OAuth.Token/Acquire",
            headers: requestHeaders,
            type: "POST",
            data: JSON.stringify(resourceData),
            success: function (data) {
                var msGraphToken = data.access_token;
                deferred.resolve(msGraphToken);                
            },
            error: function (jqxr, errorCode, errorThrown){
               console.log(jqxr.responseText);
                deferred.reject(jqxr.responseText);
            }
        });
        return deferred.promise();
}

When I first login to Microsoft Graph from my browser and then run this code I get the access token. But When code is run without explicit login I get the following error in error function:

The user is required to use multi-factor authentication

As I am using SharePoint Online site, users are already getting logged in to O365 environment. In that case(where SSO is used for all the sites), I believe there is no need toexplicitly login again to Graph. And token should be available. How to access the token information without login?

Can I reuse the token acquired in TokenFactory.js file?


Viewing all articles
Browse latest Browse all 7589

Trending Articles