我打算将文件从linux系统传输到Onedrive。但是我使用Microsoft Garph API实现了它。但是我想知道是否仅提供用户名和密码,如何获得访问权限。
供我参考的代码
import os
import requests
params = {
'grant_type': 'refresh_token',
'client_id': '',
'refresh_token': ''
}
response = requests.post('https://login.microsoftonline.com/common/oauth2/v2.0/token', data=params)
access_token = response.json()['access_token']
new_refresh_token = response.json()['refresh_token']
header = {'Authorization': 'Bearer ' + access_token}
directory='./uploads'
upload_url="https://graph.microsoft.com/v1.0/users/username/drive/root:/fotos/HouseHistory"
for root, dirs, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root,filename)
print("Uploading "+filename+"....")
fileHandle = open(filepath, 'rb')
r = requests.put(upload_url+"/"+filename+":/content", data=fileHandle, headers=header)
fileHandle.close()
if r.status_code == 200 or r.status_code == 201:
#remove folder contents
print("succeeded, removing original file...")
os.remove(os.path.join(root, filename))
print("Script completed")
raise SystemExit
如果提供了用户名和密码,如何使用API生成客户端ID和刷新令牌。除此之外,我们还需要设置权限,例如FileReadWrite
[我可以知道在代码中对脚本进行一般化需要进行哪些更改。仅通过提供用户名和密码,我需要执行所有这些操作。目前,我通过[
手动生成了客户端ID,并在此处手动设置了PERMISSIONhttps://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/Overview
感谢您的帮助
正如我在您的another post中所提到的,我们不能使用用户名和密码来获取客户端ID。我们应该在Azure门户中创建Azure AD应用,从那里获取客户端ID并将其配置到您的项目中。
我注意到您正在尝试将refresh_token
用作grant_type
。那是不对的。刷新令牌用于获取新的访问令牌。您可以参考此示例:Refresh the access token。
您应使用authorization code flow进行授权。并且您将在this step中获得刷新令牌。但这不是项目中需要的。因为此处不应将refresh_token
用作grant_type
。仅当需要刷新新的访问令牌时,才应将grant_type
设置为refresh_token
。
请参阅sample project以获取更多信息。