【问题标题】:Using Office 365 SharePoint REST API within Rails app, access token fails在 Rails 应用程序中使用 Office 365 SharePoint REST API,访问令牌失败
【发布时间】:2023-04-10 12:25:02
【问题描述】:

我正在尝试通过 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 &lt;serviceDebug&gt; 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://&lt;domain&gt;.sharepoint.com/_api/web/title", { "Authorization" =&gt; token.token }) 返回RestClient::Unauthorized: 401 Unauthorized

我对此几乎束手无策,我这个接近告诉我的老板,如果它可以以某种方式完成,我只是没有足够的知识去做它。

【问题讨论】:

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


    【解决方案1】:

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

    我不知道该服务器是如何配置的,或者根本不知道 SharePoint。但是,错误可能是任何可以想象的,甚至与 oauth 无关。除非您可以获取服务器日志或让它吐回异常,否则您将陷入困境。

    【讨论】:

    • @EugenioPace 所说的,我认为我无法改变太多,因为它是 SharePoint Online 服务器而不是本地安装。
    【解决方案2】:

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

    【讨论】:

    • (让我先说一下我多么喜欢 MSDN 文档。)我刚刚尝试在 http://sharepoint/content/sitecollection/webhttp://sharepoint/content/tenant 上授予 FullControl AppOnly 权限,只是为了好玩,我添加了 https 版本好吧。没有骰子。如果还有其他可以添加的权限范围,我还没有找到它的文档。
    • 我听到了 :-( 我没有带样本来仔细检查。我不记得确切,但我记得有相同(或非常相似)的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多