Oauth2 for SharePoint 365 REST

蹲街弑〆低调 提交于 2019-12-03 00:22:24

I've successfully connected to SharePoint Online using PHP. It looks to me like you're combining two different methods to do so. Here is what I did:

The appregnew.aspx and appprincipals.aspx pages are used for apps for SharePoint but it doesn't look like you're building one.

I think you are trying to do OAuth from Sharepoint Online without creating an Add-In inside Sharepoint. This was the exact problem I was facing. I know this is an old question but my answer might be helpful to someone else coming here.

It is possible to do OAuth from any web-app. Here are the step by step instructions in my blog -

https://medium.com/@yash_agarwal2/performing-oauth-and-rest-calls-with-sharepoint-online-without-creating-an-add-in-677e15c8d6ab#.6pf4wp83b

High Level overview -

1) Get client id and secret by registering yourself here - https://sellerdashboard.microsoft.com/Registration

2) Get Access Token, Refresh Token by following steps here -

https://msdn.microsoft.com/en-us/library/office/jj687470.aspx

Im probably a bit late to the party, but to whoever stumbles into this one, you can have a look at how the SharePoint Oauth App Client lib does it.

You'll probably want to check the SPSite and SPAccessToken classes.

I know this is old by had to deal with similar problem and have 2.5 solutions.

This solution is specifically for the federated logins.

Simple hacky solutions works like a magic for files :)

  1. On Windows 10 - the silliest solution:

    • First login to your sharepoint with Internet Exploer/Edge. Must be IE/Edge.
    • Then in Start-> Run dialog or any Windows Explorer address bar do this: \\<your_sharepoint_site.com>@SSL\rest\of_the\path. Note @SSL after host name.
    • Make sure to replace all the forward slashes with backslashes.
    • This makes sure that your login credentials are captured by the system.
    • Then navigate to "This PC", menu "Computer->Map Network Drive" and paste either that same path from above or your normal URL https://<your_sharepoint_site.com>/rest/of_the/path into Folder text field of Map Network Drive.
    • Now you have Sharepoint folder mapped as regular network drive X:\! Which allows you to treat is just as normal file system folder from any program or programming language.
    • This solution uses WebDav WebClient and by default you will hit a 50MB limit! Worry not follow these steps to release yourself from the shackles of the silly limit.
  2. Simple Hacky.

    • Open your browsers and open the 'Developer Tools'.
    • Navigate to 'Network' tab of 'Developer Tools'
    • With Network tab open login to your sharepoint site.
    • Click on any of the requests and you shall see that every single Request Header has a Cookie with FedAuth=some_very_long_base64_encoded_xml_struct;rtFa=something_else_long.
    • Basically you need these two cookies FedAuth and rtFa.
    • Now with every GET POST (curl, wget etc) request to the Sharepoint just add a new header Cookie with the value FedAuth=<fedauth-cookie-value>;rtFa=<rtFa-cookie-value>.
  3. Programmatic-hacky (example in python on windows), essentially #2 but cookie manipulation all done programmatically.

    • We shall steal the cookie directly from the kid... I mean from the Chrome browser.
    • Navigate to your Sharepoint With the chrome browser and perform all the necessary login actions.
    • Steal the cookie gist. Plagiarized from here.
    • Use the cookie gist.
    • REST of Sharepoint REST API wisdom here and ref here.
  4. Finally open a beer enjoy your day.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!