Monday, 23 July 2018

Graph Access token without login in sharepoint online




function GraphAccessToken()
{
    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;
            GetSharedWithMeDocument(msGraphToken);
            deferred.resolve(msGraphToken);
        },
        error: function (jqxr, errorCode, errorThrown) {
            console.log(jqxr.responseText);
            deferred.reject(jqxr.responseText);
        }
    });
    return deferred.promise();
}


function GetSharedWithMeDocument(token)
{
        var odurl = "https://graph.microsoft.com/v1.0/me/drive/sharedWithMe"
       // var thumbnailSize = "large"
      //  var odquery = "?expand=thumbnails,children(expand=thumbnails(select=" + thumbnailSize + "))";
        $.ajax({
            url: odurl,
            dataType: 'json',
            headers: { "Authorization": "Bearer " + token },
            accept: "application/json;odata.metadata=none",
            success: function (data)
            {
                var trCollection = "";
                if (data)
                {
                    for(var itemIndex=0;itemIndex<data.value.length;itemIndex++)
                    {                   
                        trCollection += "<tr><td>" + iconlink + "</td><td>" + data.value[itemIndex].remoteItem.shared.sharedDateTime + "</td><td>" + data.value[itemIndex].lastModifiedBy.user.displayName + "</td></tr>";
                    }
                }
                $("#oneDriveSharedWithTableBody").html('');
                $("#oneDriveSharedWithTableBody").append(trCollection);
            }           
        });
}

No comments:

Post a Comment