如何仅使用用户名和密码访问 python 的 Microsoft One Drive

How to access Microsoft One Drive with python only using username & password

我打算将文件从我的 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 并通过

在这里手动设置了 PERMISSION

https://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 to do the authorization. And you will get a refresh token in this step。但这不是您项目中需要的。因为你不应该在这里使用 refresh_token 作为 grant_type。只有当您需要刷新新的访问令牌时,才应将 grant_type 设置为 refresh_token.

请浏览 sample project 了解更多信息。

您是否正在尝试访问消费者 onedrive 帐户?或企业帐户的onedrive?他们是两个不同的东西。但是无论哪种方式。您需要使用 Azure AD 注册您的应用程序。它可以是您自己创建的,然后只允许对消费者进行授权等,但无论哪种方式。如果你想针对 azure 验证一个应用程序,它需要有一个应用程序注册。如果您的应用程序仅连接到消费者端点,那么您可以使用 azure b2c 来完成。但是是的,因为 azure 需要能够识别您的应用程序,然后才能允许您向它发送用户名和密码。