从Outlook Office REST API获取联系人计数

Getting a Count of Contacts from Outlook Office REST API - Javascript

本文关键字:联系人 获取 API Outlook Office REST      更新时间:2023-09-26

我很难区分微软的大量相互冲突的API文档:outlook.office.com, outlook.office365.com, Microsoft graph, Azure等等

我已经成功地验证并拉出了联系人,但我似乎无法得到一个计数来知道何时应该停止分页。

我一直在使用:

Access Token url: 
    https://login.microsoftonline.com/common/oauth2/v2.0/token;
Successful Contact url: 
    https://outlook.office.com/api/v2.0/me/contacts;

他们的REST API资源说我应该能够简单地调用https://outlook.office.com/api/v2.0/me/contacts/$count,但这一直返回一个正文为-1的明文MIME类型。

如果有人在这之前挣扎过,或者知道发生了什么,我很感激在正确的方向上的一点-希望能为你提供一些简单的点!


EDIT:感谢Jason在下面的帮助支持。使用沙盒,我能够用以下命令重现问题:

登录到我的帐户->收到访问令牌->对https://outlook.office.com/api/v2.0/me/contacts/$count进行GET呼叫

请求头:

GET https://outlook.office.com/api/v2.0/me/contacts/$count HTTP/1.1
Accept: text/*, application/xml, application/json; odata.metadata=none
User-Agent: PlayGroundAgent/1.0
Authorization: Bearer [standard-access-token]
client-request-id: 8f605[client-id-obscured-for-security]7289
X-AnchorMailbox: [email-address-removed-for-security on stackOverflow]
<<p> 反应/strong>
HTTP/1.1 200 OK
Transfer-Encoding: chunked
request-id: de95eaa8-95a7-40bb-b0f9-ced7270f0433
X-CalculatedBETarget: SN1PR05MB1998.namprd05.prod.outlook.com
X-BackEndHttpStatus: 200
OData-Version: 4.0
X-DiagInfo: SN1PR05MB1998
X-BEServer: SN1PR05MB1998
X-FEServer: SN1PR0501CA0035
X-MSEdge-Ref: Ref A: 657E0491C29D46978D8DD3B01B9F93A3 Ref B: DDDD64A109F4E842A8213F038BFDD5FA Ref C: Fri Aug 19 09:20:05 2016 PST
Cache-Control: private
Date: Fri, 19 Aug 2016 16:20:05 GMT
Set-Cookie: exchangecookie=6ca5fc4df96e458e8b879de61aa574ef; expires=Sat, 19-Aug-2017 16:20:05 GMT; path=/; HttpOnly
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
-1

第二次编辑:它看起来像https://outlook.office.com/api/v2.0/me/contacts?$count=true返回@odata.count": -1以及,即使它返回有效的联系人数组。


第三次编辑:工作版本(方法与上述错误相同)Url: https://outlook.office.com/api/v2.0/me/contacts/美元数

请求头:

GET https://outlook.office.com/api/v2.0/me/contacts/$count HTTP/1.1
Accept: text/*, application/xml, application/json; odata.metadata=none
User-Agent: PlayGroundAgent/1.0
Authorization: Bearer [access-token]
client-request-id: a7954db3-[client-id]-7a6e2e74dd9c
X-AnchorMailbox: [same-email-as-above]
<<p> 反应/strong>:
HTTP/1.1 200 OK
Transfer-Encoding: chunked
request-id: 8c5db16b-3023-4968-9bdc-3d5ecac12ecb
X-CalculatedBETarget: SN1PR05MB1998.namprd05.prod.outlook.com
X-BackEndHttpStatus: 200
OData-Version: 4.0
X-DiagInfo: SN1PR05MB1998
X-BEServer: SN1PR05MB1998
X-FEServer: SN1PR0501CA0019
X-MSEdge-Ref: Ref A: 0574E46DB720491FBCEF23B73428F191 Ref B: FA4529229719F069B9D019E4D53E9200 Ref C: Fri Aug 19 09:42:55 2016 PST
Cache-Control: private
Date: Fri, 19 Aug 2016 16:42:55 GMT
Set-Cookie: exchangecookie=63a1de916a4c48be88569f05ce0361a7; expires=Sat, 19-Aug-2017 16:42:55 GMT; path=/; HttpOnly
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
29

希望这些有帮助!

很抱歉听到你遇到麻烦了!这里有几件事。

你从$count电话中得到一个负数,这很麻烦。如果您访问https://oauthplay.azurewebsites.net并登录您的帐户,您会从该调用中得到相同的结果吗?

对于分页,如果您只想获得所有结果,那么最好不要依赖$count值。相反,您应该使用响应中返回的@odata.nextLink值来获取下一页。当然,如果您试图在获得所有结果之前向用户指示有多少页,$count是实现这一目标的方法。

分页由页面大小($top参数)和"游标" ($skip parameter). If you're making a call to/me/contacts with no parameters, then you're getting the default page size of 10 and default cursor of 0. You can use the $top '参数)控制,以请求每页更多的结果。

@odata.nextLink值将始终返回一个URL,您可以使用该URL根据您在$top中指定的页面大小获得下一页(如果未指定则为10)。以下是执行GET https://outlook.office.com/api/v2.0/me/contacts的值:

"@odata.nextLink": "https://outlook.office.com/api/v2.0/me/contacts/?%24skip=10"

这将跳过10个结果(基于默认页面大小为10)。

GET https://outlook.office.com/api/v2.0/me/contacts/?$top=20的值:

"@odata.nextLink": "https://outlook.office.com/api/v2.0/me/contacts/?%24top=20&%24skip=20"

如果没有更多的页面,@odata.nextLink值将不会出现在响应中。所以你可以用它作为停止分页的指示器