Python onedrivesdk-invalid_request错误

时间:2019-07-28 13:16:23

标签: python python-3.x onedrive

我想使用Python在OneDrive上上传文件并创建文件夹。因此,我从OnDrive GitHub GitHub复制了代码,在Azure上注册了我的App,复制了ID并创建了一个机密。到目前为止,一切都很好。

但是现在,如果我运行我的代码。浏览器打开,询问是否允许自动登录,并同意,然后出现此错误:

Exception: invalid_request

我认为它与redirect_uri有关,因为如果将其复制到浏览器中,将无法访问它。

这是我的代码:

import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer

redirect_uri = 'http://localhost:8080/'
client_secret = 'The secret i created on Azure'
scopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite']

client = onedrivesdk.get_default_client(
    client_id='The ID Azure created for me', scopes=scopes)

auth_url = client.auth_provider.get_auth_url(redirect_uri)

#this will block until we have the code
code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)

client.auth_provider.authenticate(code, redirect_uri, client_secret)

我也尝试使用Proxy:

import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer

from onedrivesdk.helpers import http_provider_with_proxy

redirect_uri = 'http://localhost:8080'
client_secret = 'Secret created with Azure'
client_id = 'ID id got from Azure'
scopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite']

client = onedrivesdk.get_default_client(client_id, scopes=scopes)

auth_url = client.auth_provider.get_auth_url(redirect_uri)

code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)

proxy = {
    'http': 'http://localhost:8888',
    'https': 'https://localhost:8888'
}
http = http_provider_with_proxy.HttpProviderWithProxy(proxy, verify_ssl=True)
auth = onedrivesdk.AuthProvider(http, client_id, ['onedrive.readwrite'])
client = onedrivesdk.OneDriveClient(redirect_uri, auth, http)

f = onedrivesdk.Folder()
i = onedrivesdk.Item()
i.name = 'New Folder'
i.folder = f

returned_item = client.item(drive='me', id='root').children.add(i)

这给了我这个错误信息:

RuntimeError: Session must be authenticated 
            before applying authentication to a request.

1 个答案:

答案 0 :(得分:0)

您的代码有效-例如它发送您要发送的信息。但是,您输入的凭据当然会返回无效的请求-您尝试使用以下方式连接到Azure:

client_id: 'The ID Azure created for me'

我很确定不存在。问题是您需要一个帐户并向您的脚本传递这些(有效)帐户信息才能连接到该帐户。

相关问题