Javascript 如何在 sharepoint 2013 中获取 oauth 访问令牌?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11804624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get an oauth access token in sharepoint 2013?
提问by Luis Valencia
I have this site here: http://msdn.microsoft.com/en-us/library/jj164022(v=office.15).aspx
我在这里有这个网站:http: //msdn.microsoft.com/en-us/library/jj164022(v=office.15).aspx
the text in some part says: The following JavaScript code demonstrates how to make this GET request that returns a JSON representation of all of a site's lists by using JQuery. It also assumes that you have a valid OAuth access token that is stored in the accessToken variable. You do not need the access token if you make this call from inside an app web, as you would in a SharePoint-hosted app.
某些部分的文字说:以下 JavaScript 代码演示了如何使用 JQuery 发出此 GET 请求,该请求返回所有站点列表的 JSON 表示。它还假设您有一个存储在 accessToken 变量中的有效 OAuth 访问令牌。如果您从应用程序 Web 内部进行此调用,则不需要访问令牌,就像在 SharePoint 托管的应用程序中一样。
jQuery.ajax({
url: http:// site url/_api/web/lists,
type: "GET",
headers: {
"ACCEPT","application/json;odata=verbose",
"Authorization", "Bearer " + accessToken
},
})
the specific question is how can I get the access token??
具体问题是如何获取访问令牌?
采纳答案by Radu Simionescu
Your entire approach is incorrect. When it comes to accessing the REST services of a sharepoint 2013 server using javascript you don't need an Access Token. You have to use the Sharepoint 2013 cross-domain library instead.
你的整个方法是不正确的。在使用 javascript 访问 sharepoint 2013 服务器的 REST 服务时,您不需要访问令牌。您必须改用 Sharepoint 2013 跨域库。
The page at http://msdn.microsoft.com/en-us/library/jj164022.aspxexplains after showing a classic REST request using an oauth token:
http://msdn.microsoft.com/en-us/library/jj164022.aspx 上的页面在使用 oauth 令牌显示经典 REST 请求后进行了解释:
This request would look a little different if you are writing your app in JavaScript but using the SharePoint 2013 cross-domain library. In this case, you don't need to provide an access token. The following code demonstrates how this request would look if you are using the cross-domain library and want to receive the OData representation of the lists as XML instead of JSON. See How to: Access SharePoint 2013 data from remote apps using the cross-domain library for more information about using the cross-domain library.
如果您使用 JavaScript 编写应用程序但使用 SharePoint 2013 跨域库,则此请求看起来会有所不同。在这种情况下,您不需要提供访问令牌。以下代码演示了如果您使用跨域库并希望以 XML 而不是 JSON 的形式接收列表的 OData 表示形式,此请求的外观。有关使用跨域库的详细信息,请参阅如何:使用跨域库从远程应用程序访问 SharePoint 2013 数据。
This is currently the url to that article http://msdn.microsoft.com/en-us/library/fp179927.aspx
这是目前该文章的 网址 http://msdn.microsoft.com/en-us/library/fp179927.aspx
This should be your correct answer
这应该是你的正确答案
回答by Mark S.
There doesn't seem to be a way to get the access token only using JavaScript. You need to use an app to generate the access token. See the Sharepoint OAuth Tips and FAQsfor more information about how to get the token.
似乎没有办法仅使用 JavaScript 来获取访问令牌。您需要使用应用程序来生成访问令牌。有关如何获取令牌的更多信息,请参阅Sharepoint OAuth 提示和常见问题解答。
回答by Sundara Prabu
- This answers the question for sharepoint online.
- It should be similar
for any SharePoint list with OAuth2
This talks about Postman or Fiddler, but we can modify this for JavaScript / JQuery
- 这回答了在线共享点的问题。
- 对于任何带有 OAuth2 的 SharePoint 列表,它应该是相似的
这讨论的是 Postman 或 Fiddler,但我们可以针对 JavaScript / JQuery 修改它