我正在嘗試開發一個簡單的後臺應用程序來連接到我的onedrive帳戶(工作)並定期下載一些文件。Microsoft Graph API BadRequest當前身份驗證的上下文無效
我已經註冊了這裏的應用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',
}
我回來的access_token
{'ext_expires_in': 0,
'token_type': 'Bearer',
'expires_in': 3600,
'access_token': 'eyJ0eXAiOiJKV1QiLCJhbGciO---SHORTENED FOR BREVITY'}
接下來我做一個GET請求(與Bearer
頭正確設置),以https://graph.microsoft.com/v1.0/me
,並得到這個eror反應(這是我獲得的任何端點FWIW)
{
"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"
}
}
}
任何想法可能是錯的?
丹,謝謝!我已閱讀了關於OAuth2的更多信息,並在此處看到此問題。 'client_credentials'和'authorization_code'是唯一支持的流程嗎?爲了我的目的,我更願意使用委託權限,因爲我真的只需要訪問用戶可以看到的文件,我不需要(也不想)訪問組織的所有文件。唯一的方法是使用涉及使用'refresh_tokens'的'authorization_code'嗎?有沒有辦法得到某種長壽的'refresh_token'或使用'grant_type = password'? –
請按照此處的指導進行操作:https://developer.microsoft.com/en-us/graph/docs/concepts/auth_v2_user獲取授權代碼流,並使用持久刷新令牌。請不要使用密碼流 - 這個流程有非常特定的場景,它帶有一系列嚴重的缺點 - 請參閱http://www.cloudidentity.com/blog/2014/07/08/using-adal-淨值與身份驗證用戶,通過-usernamepassword /。 –