在Rails应用程序中使用Office 365 SharePoint REST API,访问令牌失败

时间:2014-06-28 19:54:41

标签: rest ruby-on-rails-4 ms-office sharepoint-2013 office365

我正在尝试通过SharePoint REST API将Office 365组织SharePoint网站用作存储点,以用于在Rails应用中生成的docx文件。我已通过https://<domain>.sharepoint.com/_layouts/15/appregnex.aspx注册了应用,并获得了client_id和client_secret。使用https://<domain>.sharepoint.com/_layouts/15/appinv.aspx,我为应用提供了以下权限:

<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />
</AppPermissionRequests>

使用oauth2 gem,我到目前为止设法使用client_credentials获取访问令牌:

client = OAuth2::Client.new(<client_id>, <client_secret>, site: "https://<domain>.sharepoint.com", token_url: "https://login.windows.net/<tenant_id>/oauth2/token")
token = client.get_token(grant_type: "client_credentials", client_id: client.id, client_secret: client.secret, resource: "https://<domain>.sharepoint.com")

当收到令牌时,client_credentials策略似乎有效。但是,尝试使用它会导致失败。

token.get("/_api/web/title")返回OAuth2::Error: { "error_description" : "The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs." }

RestClient.get("https://<domain>.sharepoint.com/_api/web/title", { "Authorization" => token.token })返回RestClient::Unauthorized: 401 Unauthorized

我非常关注这个问题,而且我这个接近告诉我的老板,如果能够以某种方式完成,我根本就没有足够的知识去做它

2 个答案:

答案 0 :(得分:0)

IncludeExceptionDetailInFaults是端点应用程序web.config中的wcf服务设置。如果您有权访问服务器https://.sharepoint.com,那么您可以暂时启用它。基本上,这将在响应中将异常跟踪转储发送到客户端。默认情况下,任何500状态错误都将返回,除了... ooops发生错误之外没有任何信息。

我根本不知道该服务器是如何配置的,或者根本不知道。然而,错误可能是任何可以想象的,甚至与oauth无关。除非您可以获取服务器日志或让它吐出异常,否则您将会转动轮子。

答案 1 :(得分:0)

很可能您没有足够的权限。尝试使用不同的范围。