doudeng8691
2018-01-25 11:07
浏览 67
已采纳

使用REST API为Azure CosmosDB获取文档

I'm trying CosmosDB Emulator using REST API from PHP. For that I'm using AzureDocumentDB-PHP.

This wrapper library is referring to the MSDN documentation for the REST API. I'm seeing a difference between MSDN documentation and docs.microsoft.com

For example, to retrieve a single document MSDN says use the following syntax:

https://{databaseaccount}.documents.azure.com/dbs/{_rid-db}/colls/{_rid-col}/docs/{_rid-doc}

Whereas the docs.microsoft.com says use the below syntax:

https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs/{doc-id}

Note the difference, MSDN uses _rid (system generated id), where as docs.microsoft.com uses just the id (user generated id).

The mentioned PHP library which is using MSDN syntax works fine with the _rid (system generated id). If I try to use the user generated id, I get the following error (for trying to get a db):

{"code":"Unauthorized",
 "message":"The input authorization token can't serve the request. 
            Please check that the expected payload is built as per the 
            protocol, and check the key being used. Server used the
            following payload to sign: 
            'get
dbs
dbs/db_test
thu, 25 jan 2018 10:28:30 gmt

'

              ActivityId: a43be1a0-c35b-4438-bb83-522204d3f589, 
             Microsoft.Azure.Documents.Common/1.19.102.5"}' (length=388)

图片转代码服务由CSDN问答提供 功能建议

我正在使用PHP的REST API尝试使用CosmosDB模拟器。 为此,我正在使用 AzureDocumentDB-PHP

此包装器库引用REST API的MSDN文档。 我发现MSDN文档与docs.microsoft.com之间存在差异

例如,要检索单个文档 MSDN 说使用以下语法:

  https:// {databaseaccount}  .documents.azure.com / dbs / {_ rid-db} / colls / {_ rid-col} / docs / {_ rid-doc} 
   
 
 

而< a href =“https://docs.microsoft.com/en-us/rest/api/documentdb/get-a-document"rel =”nofollow noreferrer“> docs.microsoft.com 说使用下面的内容 语法:

 <代码>的https:// {databaseaccount} .documents.azure.com / DBS / {分贝-ID} / colls / {COLL-ID} /文档/ {DOC  -id} 
   
 
 

注意区别,MSDN使用 _rid (系统生成的id),其中docs.microsoft.com仅使用 id (用户生成的id)。

使用MSDN语法的上述PHP库与_rid(系统生成的id)一起正常工作。 如果我尝试使用用户生成的id,我会收到以下错误(尝试获取数据库):

  {“code”:“Unauthorized”,
“ 消息“:”输入授权令牌无法提供请求。
请检查是否按照
协议构建了预期的有效负载,并检查正在使用的密钥。服务器使用以下有效负载签名:\  n'get 
dbs 
dbs / db_test 
thu,25 jan 2018 10:28:30 gmt 
 
 
 
 
 ActivityId:a43be1a0-c35b-4438-bb83-522204d3f589,
 Microsoft.Azure。  Documents.Common / 1.19.102.5“}'(长度= 388)
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • du94414 2018-01-26 02:19
    已采纳

    Actually,The MSDN doc and doc.microsoft.com are both cosmosdb REST APIs.

    However , the MSDN doc is stopped updating in July 14, 2015.Obviously, the Azure Cosmos DB REST API is constantly being updated and optimized, as shown in the x-ms-version parameter which is required in the request header.

    The AzureDocumentDB-PHP uses the old version of Azure Cosmos DB REST API which supports _rid of resources , so you can not use it with id of resources. The x-ms-version parameters are different.

    Hope it helps you.

    打赏 评论
  • douying8666 2018-03-09 20:45

    I'm an author of AzureDocumentDB-PHP.

    This is because of creating an authorize token problem. Resource Link should be like "dbs/db_test" when with id of resource.

    See next link and Example Encoding. https://docs.microsoft.com/ja-jp/rest/api/documentdb/access-control-on-documentdb-resources?redirectedfrom=MSDN#constructkeytoken

    I will fix this issue in future.

    打赏 评论

相关推荐 更多相似问题