python - Office 365 'app only' token 请求失败,出现 'invalid signature' 错误

标签 python office365

我正在尝试执行“客户端凭据”流程并获取 Office 365 应用程序的访问 token 。我按照以下步骤操作。

第 1 步:我按照以下文档在 azure AD 上注册了该应用程序 http://blogs.msdn.com/b/exchangedev/archive/2015/01/21/building-demon-or-service-apps-with-office-365-mail-calendar-and-contacts-apis-oauth2-client-credential-flow.aspx?CommentPosted=true#commentmessage

第2步:为我的应用程序配置X.509公共(public)证书。下载 list 文件并更新“KeyCredentials”值,然后再次上传 list (按照步骤 1 文档进行操作)。

"keyCredentials": [
    {
      "customKeyIdentifier": "BB/OLYBBivt4RXUKxQbk+c9UEmo=",
      "endDate": "2016-12-15T07:00:00Z",
      "keyId": "6837d367-660c-4d44-b0ea-9dcd538828e5",
      "startDate": "2014-12-15T07:00:00Z",
      "type": "AsymmetricX509Cert",
      "usage": "Verify",
      "value": null
    }

步骤3: 按照中的步骤操作 Office 365 Rest API - Daemon week authentication生成“客户端断言”,如下所示。

base64(header).base64(payload).base64(siganature)

标题如下:

{ 
  "alg": "RS256",
  "x5t": "BB/OLYBBivt4RXUKxQbk+c9UEmo=" //same as customKeyIdentifier
}

有效负载为:

{
  "aud": "https://login.windows.net/<tenantid>/oauth2/token",
  "exp": "now + 1 hour",
  "nbf": "now",
  "iss": "CLIENT ID",
  "jti": "SOME GUID",
  "sub": "CLIENT ID"
}

对于签名(RSA-SHA-256算法),我使用了openssl,如下所示。

openssl dgst -sha256  -sign privatekey.pem < input.txt  > signature

注意:input.txt包含base64(header).base64(payload)

第四步: 当我请求获取访问 token 时,出现“无效签名”错误,如下所示

{
    "error": "invalid_client",
    "error_description": "AADSTS70002: Error validating credentials. AADSTS50012: Client assertion contains an invalid signature.\r\nTrace ID: 880b11db-0935-4cbd-836e-4d4d91b2a9e2\r\nCorrelation ID: e72ae39a-9868-4efe-847e-890ef32d48ae\r\nTimestamp: 2015-03-12 18:45:54Z",
    "error_codes": [
        70002,
        50012
    ],
    "timestamp": "2015-03-12 18:45:54Z",
    "trace_id": "880b11db-0935-4cbd-836e-4d4d91b2a9e2",
    "correlation_id": "e72ae39a-9868-4efe-847e-890ef32d48ae",
    "submit_url": null,
    "context": null
}

我在 RSA SHA256 签名中做错了什么?

任何帮助将不胜感激。

注意:我什至在python中使用“PyJWT”,在nodejs中使用“jsonwebtoken”来生成客户端断言(JWT token )并将其用于 token 请求,我得到了相同的错误。

最佳答案

当您下载 list 时,它始终会发送 value 键的空值。这并不表明有问题。确保您正在执行 RSA SHA-256 签名。请参阅Could not retrieve app only tokens for office 365

关于python - Office 365 'app only' token 请求失败,出现 'invalid signature' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29019549/

相关文章:

python - 从 Perl 脚本运行 Python 脚本

Python 多处理作业到 Celery 任务但 AttributeError

python - 获取 IndexError : string index out of range in python

excel - 在 VBA 或 Excel 中,如果数组包含值,我如何遍历数组并用 1 填充一些单元格,如果不包含值,则用 0 填充?

office365 - 通过统一 API 访问 SharePoint Online 网站

python - python 中的 __str__ 方法

Python SimpleHTTPServer 提供错误的index.html?

excel - 向 Excel 调查提交(响应)的电子邮件通知

powershell - 在单独的列中导出365用户邮箱的大小和单位

c# - 微软图形 API : Getting error "Authorization_IdentityNotFound"