我是一名有用的助手,可以为您翻译文本。
使用GitHub上的代码示例,专门设置Python访问OneDrive API的身份验证(我开始认为此来源已过时),但我未能通过在执行程序后粘贴Microsoft提供的代码部分。
Python代码:
import onedrivesdk
redirect_uri = 'https://login.microsoftonline.com/common/oauth2/nativeclient'
client_secret = '*this code omitted*'
client_id='*this code omitted*'
api_base_url='https://api.onedrive.com/v1.0/'
scopes=['onedrive.readwrite']
http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider(
http_provider=http_provider,
client_id=client_id,
scopes=scopes)
client = onedrivesdk.OneDriveClient(api_base_url, auth_provider, http_provider)
auth_url = client.auth_provider.get_auth_url(redirect_uri)
# Ask for the code
print('Paste this URL into your browser, approve the app\'s access.')
print('Copy everything in the address bar after "code=", and paste it below.')
print(auth_url)
code = raw_input('Paste code here: ')
client.auth_provider.authenticate(code, redirect_uri, client_secret)
执行代码并在浏览器中粘贴URL后,弹出一个窗口,我确认我想要给我的应用程序访问API的权限。我点击“确定”。
然后,在URL任务栏中会显示代码。我将代码复制并粘贴到程序中。
然后我得到的错误是:
raise Exception(str(message["error"]))
Exception: invalid_request
使用的GitHub源链接:https://github.com/OneDrive/onedrive-sdk-python
注意:我不得不省略此列表中的前两个范围:
scopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite']
因为它们显然不存在(根据微软提供的错误代码,在将URL粘贴到任务栏后)
有没有更好的来源来设置身份验证,以便Python程序与OneDrive API通信?
我是一个相对较新的Python用户,感谢您的耐心。