SharePoint OAuth

by dshilkret

HTML

<span>Div1</span>
<div id='div1'>
Test1
</div><br>
<span>Div2</span>
<div id='div2'>
Test2
</div>
<div id='div3'>
Test3
</div>

JavaScript

$.ajax({
    type: 'POST',
    crossDomain: true,
    url: 'https://accounts.accesscontrol.windows.net/2d94daf0-3519-4a12-bacf-9e5a9bf37417/tokens/OAuth/2',
    headers: {
        "content-type": "application/x-www-form-urlencoded"
    },
    data: {
        "grant_type": "client_credentials",
        "client_id": "12e52415-01fe-48b1-9578-1d72b0194f17@2d94daf0-3519-4a12-bacf-9e5a9bf37417",
        "client_secret": "qkiOE773*!qnqrODCRE60(^",
        "resource": "00000003-0000-0ff1-ce00-000000000000/sbrokers.sharepoint.com@2d94daf0-3519-4a12-bacf-9e5a9bf37417"
    },
    success: function(data) {
        //data.token_type returns "Bearer"
        //data.access_token returns < AccessToken >
        var btok = data.token_type + " " + data.access_token;
        $("#div1").html("<span>"+btok+"</span>");
        $.ajax({
          type: 'POST',
          crossDomain: true,
          url: "https://sbrokers.sharepoint.com/_api/web/getfolderbyserverrelativeurl('LIST1')/files" ,
          headers: {
          		"accept":"application/json",
              "content-type": "application/x-www-form-urlencoded",
              "Authorization": btok
          },
          success: function(data) {
          for (var d in data.value) {
          		 $("#div2").html("<span>"+JSON.stringify(data.value[d].ServerRelativeUrl)+"</span>");
               $("#div3").html("<br><span><a target='_blank' href='https://sbrokers.sharepoint.com" + ServerRelativeUrl +"?web=1" + "&action=edit" + "'" +">File Link</a></span>");
}


    
         
          },
          error: function(data, errorThrown, status) {
							$("#div2").html("<span>"+  errorThrown+"</span>");
    			}
        });
      //caal the REST API with the at variable in header

    },
    error: function(data, errorThrown, status) {
							$("#div1").html("<span>"+errorThrown+"</span>");
    }
});