TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Gowtham
NA
516
40.6k
Get access token form Azure Directory for Microsof Graph API
Feb 20 2019 12:52 AM
Hi there,
I have requirement to fetch planner details on sharepoint custom pages.As per the azure end points I have created the token but it is not valid one.I dont find any other refrence for clinet side to authenthicate the credentials.
So for I tried
<!DOCTYPE>
<html>
<head>
<title>Sample For Browser</title>
<script type=
"text/javascript"
src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"
></script>
<script type=
"text/javascript"
src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"
></script>
<script type=
"text/javascript"
src=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
></script>
<script type=
"text/javascript"
src=
"https://secure.aadcdn.microsoftonline-p.com/lib/1.0.10/js/adal.min.js"
></script>
<script type=
"text/javascript"
>
$(
function
(){
console.log(
'Loading'
);
var
accessToken =
""
;
var
space = 20;
var
child = 0;
var
configOptions = {
clientId:
""
,
//Required clientId to get accessToken
postLogoutRedirectUri: window.location.origin,
redirectURI: window.location.href,
// Your page link
//cacheLocation: "localStorage", //Optional to save data in localStorage by defualt it store in session storage
}
window.authContext =
new
AuthenticationContext(configOptions);
if
(authContext.isCallback(window.location.hash))
{
authContext.handleWindowCallback();
//Using this we can get user from getCachedUser() after login
}
var
user = authContext.getCachedUser();
if
(user ==
null
){
authContext.login();
//Login if user not found in getCachedUser
}
else
{
//setTimeout(function(){ getToken(); }, 1000);
//setTimeout(function(){ getUser(); }, 3000);
getToken();
}
function
getToken(){
//get accessToken
authContext.acquireToken(
"https://graph.microsoft.com"
,
function
(error, token){
console.log(
"Error ="
+error);
console.log(
"Token ="
+token);
accessToken = token;
if
(accessToken && accessToken !=
""
)
{
getOnedriveUser();
}
})
}
function
getOnedriveUser(URL){
// get data based on passed url
$.ajax({
url: URL?URL:
"https://graph.microsoft.com/v1.0/me/planner/tasks"
,
type:
'GET'
,
headers: {
"Authorization"
:
"Bearer "
+ accessToken
},
async:
false
,
success:
function
(data) {
console.log(data);
allData = data.value;
$.each(allData,
function
(e,item){
item.parentReference.path.split(
'root:'
)[1].split(
'/'
).length == 0?child = 0:child = item.parentReference.path.split(
'root:'
)[1].split(
'/'
).length;
$(
'#oneDriveData tbody'
).append(
'<tr><td'
+(URL?
' style="padding-left:'
+space*child+
'px"'
:
""
)+
'>'
+
(item.file?
"<i class='fa fa-file'></i>"
:
"<i class='fa fa-folder'></i>"
)+
'</td>'
+
'<td>'
+(item.name?item.name:
" "
)+
'</td>'
+
'<td>'
+
new
Date(item.lastModifiedDateTime).format(
'dd/MM/yyyy'
)+
'</td>'
+
'<td>'
+
new
Date(item.lastModifiedDateTime).format(
'dd/MM/yyyy'
)+
'</td></tr>'
);
if
(item.folder)
{
//child++;
var
url =
"https://graph.microsoft.com/v1.0/me/drive/items/"
+item.id+
"/children"
;
getOnedriveUser(url);
}
})
},
error:
function
(error)
{
alert(
"Error"
+ error);
}
});
//ajax
}
//get user
})
</script>
</head>
<body>
<div
class
=
"container-fluide"
>
<h1>OneDrive</h1>
<table id=
"oneDriveData"
class
=
"table table-striped"
>
<thead>
<tr>
<th>Folder OR File</th>
<th>Name</th>
<th>Created Date</th>
<th>Modify Date</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</body>
</html>
Through client id i tried to get mircrosoft planner using Microsoft Graph api. Even though getting acess token But I am getting error on https://graph.microsoft.com/v1.0/me/planner/tasks 403 (Forbidden);
can any on suggest me based on this link I reffered https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/samples/browser/src/index.html.
Reply
Answers (
2
)
customize the add an app page(addanapp.aspx) in SharePoint13
How to add lookup fields data from excel to SharePoint List