我有一个场景,我需要连接 onedrive 并获取所有文件夹和内部文件信息,因此我在 azure 中注册了应用程序以获取 tenat id 和客户端 id 等...
我成功登录并能够获取文件夹和文件信息,但我有大约 8k+ 文件夹和内部文件,所以我在收到该错误 1 到 2 小时后得到令牌过期时间,我也有刷新令牌但不确定我如何在 GET 请求中使用该令牌
我在这里粘贴代码,任何人都可以建议如何使用它,在我的代码中,我正在创建具有 access_token 和 refresh_token 的 bin 文件
TENANT_ID = '111*********f5d4'
CLIENT_ID = '7c0*************5517'
AUTHORITY = 'https://login.microsoftonline.com/' + TENANT_ID
ENDPOINT = 'https://graph.microsoft.com/v1.0'
base_url = 'https://graph.microsoft.com/v1.0/'
SCOPES = [
'Files.ReadWrite.All',
'Sites.ReadWrite.All',
'User.Read',
'User.ReadBasic.All'
]
cache = msal.SerializableTokenCache()
if os.path.exists('token_cache.bin'):
cache.deserialize(open('token_cache.bin', 'r').read())
atexit.register(lambda: open('token_cache.bin', 'w').write(cache.serialize()) if cache.has_state_changed else None)
app = msal.PublicClientApplication(CLIENT_ID, authority=AUTHORITY, token_cache=cache)
accounts = app.get_accounts()
result = None
if len(accounts) > 0:
result = app.acquire_token_silent(SCOPES, account=accounts[0])
if result is None:
flow = app.initiate_device_flow(scopes=SCOPES)
if 'user_code' not in flow:
raise Exception('Failed to create device flow')
print(flow['message'])
result = app.acquire_token_by_device_flow(flow)
if 'access_token' in result:
parent = base_url +'/drives/!FJ9b********************BnEOcC-mq/root:/Box_Migration/Product Images - SHARE'
link = parent+":/children"
while True:
rGetCh = requests.get(link, headers={'Authorization': 'Bearer ' + result['access_token']})
data=rGetCh.json()['value']
for itemm in data:
file_id =itemm['name']
print(file_id)
endpoint=base_url + f'/drives/b!FJ9b********************BnEOcC-mq/root:/Box_Migration/****uct I888ages - SHARE/{file_id}:/children'
response_data= requests.get(endpoint,headers={'Authorization': 'Bearer ' + result['access_token']})
print(response_data)
json_data =response_data.json()
#print(json_data)
with open('new_Refresh_token.json', 'a+') as fout:
fout.write(json.dumps(json_data,indent = 4,default=str))
fout.write(',\n')
fout.close()
for ch in rGetCh.json()["value"]:
# Looping through the current list of children
chName = urllib.parse.quote(ch["name"].encode('utf8'))
chPath = parent + "/" + chName
if "@odata.nextLink" in rGetCh.json(): # if this is in the response, there are more children
link = rGetCh.json()["@odata.nextLink"]
else:
break
else:
raise Exception('no access token in result')
这里我们有 2 个 GET 请求,一个用于获取文件夹信息,另一个用于获取文件信息
任何人都可以建议/帮助/想法,这是非常高的优先级