我正在尝试开发一个简单的后台应用程序以连接到我的onedrive帐户(工作)并定期下载一些文件。
我已经在这里注册了该应用程序https://apps.dev.microsoft.com/portal/register-app
我写下了client_id
和client_secret
要获取访问令牌,我向发出POST请求
https://login.microsoftonline.com/common/oauth2/v2.0/token 与以下表单编码数据
{
'client_id': 'clientid here',
'client_secret': 'secret is here',
'scope': 'https://graph.microsoft.com/.default',
'grant_type': 'client_credentials',
}
Run Code Online (Sandbox Code Playgroud)
我回来了 access_token
{'ext_expires_in': 0,
'token_type': 'Bearer',
'expires_in': 3600,
'access_token': 'eyJ0eXAiOiJKV1QiLCJhbGciO---SHORTENED FOR BREVITY'}
Run Code Online (Sandbox Code Playgroud)
接下来,我Bearer
向https://graph.microsoft.com/v1.0/me发出一个GET请求(正确设置了标题)
并得到这个错误的响应(对于任何端点,我都会得到)
{
"error": {
"code": "BadRequest",
"message": "Current authenticated context is not valid for this request",
"innerError": {
"request-id": "91059f7d-c798-42a1-b3f7-2487f094486b",
"date": "2017-08-05T12:40:33"
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何想法可能有什么问题吗?
我将提交一个错误来改进此可怕的错误消息。问题是您正在使用应用程序权限(client_credentials流)发出请求-没有登录的用户上下文。您的请求是/me
,/me
基本上是已登录用户的别名-在这种情况下,没有一个!
您应该尝试致电https://graph.microsoft.com/v1.0/users
。 但是,在您这样做之前。在应用程序注册门户中,您已经选择了委派权限,但是您正在使用应用程序权限进行调用。您应该删除委派的权限,然后选择适当的应用程序权限-要调用用户,请选择User.Read.All。然后,通过转到/ adminconsent端点来确保同意/同意您的应用程序。
也请在此处阅读有关权限以及委派和应用程序权限的更多信息:https : //developer.microsoft.com/zh-cn/graph/docs/concepts/permissions_reference
希望这可以帮助,
归档时间: |
|
查看次数: |
3277 次 |
最近记录: |