python - 身份验证 OneDrive API Python

标签 python api authentication onedrive

使用来自 GitHub 的代码示例,专门用于为 Python 访问 OneDrive API 设置身份验证(我开始认为此源已过时),我未能通过粘贴由提供的代码的部分微软在执行程序后..

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 用户,感谢您的耐心等待。

最佳答案

我遇到了同样的问题,解决方案是在应用注册中包含 redirect_uri。

这可以在 https://portal.azure.com/ 完成und Azure Active Directory > 应用程序注册 > “你的应用程序” > 身份验证。就我而言,我需要添加 http://localhost:8080/到重定向 URI。

我在这里找到了建议:

https://github.com/OneDrive/onedrive-sdk-python/issues/98

希望它能帮助人们节省一些时间。

关于python - 身份验证 OneDrive API Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51509720/

相关文章:

json - WSO2 API Manager Auth 错误内容类型

ios - 如何使用 token 对用户进行身份验证(在 iPhone 中保持身份验证)

python - Docker Compose Volumes 不推送最新代码

python - 如何从多对多表中检索字段?

python - 有没有办法默认在 Python 中启用不一致的空格检查(-t)

api - Yii2 Restful API - 添加新操作的示例

java - Reddit Api unsupported_grant_type 错误与 Retrofit (java)

Python:使用 pandas 将一个数组连接到另一个数组

c# - 如何将 Bing Speech API 与 Bot Framework C# 结合使用

java - Spring 安全 : saved SecurityContext got a Null authentication