我正在編寫一個Python腳本來編輯生活在SharePoint中的Excel電子表格。我們有Office 365 for Business。我正在使用Microsoft Graph API。 我向Microsoft Azure Active Directory(AD)註冊了Python應用程序,併爲Graph API添加了以下2個(僅適用於應用程序)權限:(1)讀取和寫入所有網站集中的文件(預覽)和(2)讀取目錄數據。 (我有我們公司管理員註冊應用程序對我來說)。Azure AD:訪問令牌響應正文丟失的「範圍」屬性
我的Python腳本使用請求庫發送REST請求:
import json
import requests
MSGRAPH_URI = 'https://graph.microsoft.com'
VERSION = 'v1.0'
def requestAccessToken(tenant_id, app_id, app_key):
MSLOGIN_URI = 'https://login.microsoftonline.com'
ACCESS_TOKEN_PATH = 'oauth2/token'
GRANT_TYPE = 'client_credentials'
endpoint = '{0}/{1}/{2}'.format(MSLOGIN_URI, tenant_id, ACCESS_TOKEN_PATH)
headers = {'Content-Type': 'Application/Json'}
data = {
'grant_type': GRANT_TYPE,
'client_id': app_id,
'client_secret': app_key,
'resource': MSGRAPH_URI
}
access_token = response.json()['access_token']
return access_token
def getWorkbookID(access_token, fileName):
endpoint = '{0}/{1}/me/drive/root/children'.format(MSGRAPH_URI, VERSION)
headers = {
'Content-Type': 'Application/Json',
'Authorization': 'Bearer {}'.format(access_token)
}
response = requests.get(endpoint, headers=headers)
print response.text
assert response.status_code == 200
items = response.json()['value']
workbook_id = None
for item in items:
if item['name'] == fileName:
workbook_id = item['id']
return workbook_id
access_token = requestAccessToken(TENANT_ID, APP_ID, APP_KEY)
workbook_id = getWorkbookID(access_token, WORKBOOK_FILENAME)
的Python應用程序成功地請求,並從微軟服務器接收到一個access_token
。訪問令牌啓動這樣
eyJ0eXAiOiJKV1QiLCJub25jZSI6...
然後要求我的檔案在getWorkbookID()的列表:
GET https://graph.microsoft.com/v1.0/me/drive/root/children
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6...
這是該REST請求的響應:
{
"error": {
"code": "InternalServerError",
"message": "Object reference not set to an instance of an object.",
"innerError": {
"request-id": "aa97a822-7ac5-4986-8ac0-9852146e149a",
"date": "2016-12-26T22:13:54"
}
}
}
注當我通過圖形瀏覽器(https://graph.microsoft.io/en-us/graph-explorer)請求時,我成功獲取了我的文件列表。
編輯:
- 從「微軟圖形API返回對象引用不設置到對象的實例」到「Azure的AD‘範圍’從訪問令牌響應丟失」改了稱呼。
- 更改了 「我」 的GET請求 「myOrganization」 的URI,看完這個:graph.microsft.io/en-us/docs/overview/call_api
也就是說,
GET https://graph.microsoft.com/v1.0/myOrganization/drive/root/children
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6...
現在得到以下回應。
{
"error": {
"code": "AccessDenied",
"message": "Either scp or roles claim need to be present in the token.",
"innerError": {
"request-id": "bddb8c51-535f-456b-b43e-5cfdf32bd8a5",
"date": "2016-12-28T22:39:25"
}
}
}
綜觀graph.microsoft.io/en-us/docs/authorization/app_authorization一個例子,我看到訪問令牌響應體內含有一種「範圍」屬性,用於列出授予應用程序的權限在應用程序的註冊過程中。但是,從服務器接收的訪問令牌響應不具有「範圍」屬性。這是我的訪問令牌響應的樣子。
{
"token_type":"Bearer",
"expires_in":"3599",
"ext_expires_in":"0",
"expires_on":"1482968367",
"not_before":"1482964467",
"resource":"https://graph.microsoft.com",
"access_token":"eyJ0eXAiOiJKV..."
}
問題:
- 我有管理員登記在Azure的AD應用程序,並檢查盒爲所需的Microsoft Graph API應用程序的權限。顯然這還不夠。還需要什麼?爲什麼權限不在訪問令牌響應正文中?
- GET請求的正確URI是什麼? 「MyOrganization」是URI中的正確值嗎?
錯誤是.NET中經典的NullReferenceException。可能是API中的一個錯誤,或者是它預期的錯誤。似乎不是一個權限問題。 – juunas