【问题标题】:python3 upload files to ondrive or sharepoint?python3将文件上传到onedrive或sharepoint?
【发布时间】:2017-01-03 16:37:32
【问题描述】:

有人知道这是否可能吗? 我只想自动将一些文档放入我的 onedrive for business 帐户中。

我试过了

                import onedrivesdk
                from onedrivesdk.helpers import GetAuthCodeServer
                from onedrivesdk.helpers.resource_discovery import ResourceDiscoveryRequest

            redirect_uri = 'http://localhost:8080'
            client_id = 'appid'
            client_secret = 'mysecret'
            discovery_uri = 'https://api.office.com/discovery/'
            auth_server_url='https://login.live.com/oauth20_authorize.srf?scope=wl.skydrive_update'
            #auth_server_url='https://login.microsoftonline.com/common/oauth2/authorize',
            auth_token_url='https://login.microsoftonline.com/common/oauth2/token'

            http = onedrivesdk.HttpProvider()
            auth = onedrivesdk.AuthProvider(http,
                                            client_id,
                                            auth_server_url=auth_server_url,
                                            auth_token_url=auth_token_url)
            auth_url = auth.get_auth_url(redirect_uri)
            code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)
            auth.authenticate(code, redirect_uri, client_secret, resource=resource)
            # If you have access to more than one service, you'll need to decide
            # which ServiceInfo to use instead of just using the first one, as below.
            service_info = ResourceDiscoveryRequest().get_service_info(auth.access_token)[0]
            auth.redeem_refresh_token(service_info.service_resource_id)
            client = onedrivesdk.OneDriveClient(service_info.service_resource_id + '/_api/v2.0/', auth, http)

我注册了一个APP,得到了一个secret和id。但是当我运行这个时,我得到了 scope is invalid 错误。另外,它会尝试启动一个不适合命令行环境的网页。我认为这个 SDK 也可能已经过时了,因为最初这个脚本有 login.microsoftonline,但无法访问,所以我将其更改为 login.live.com。

【问题讨论】:

    标签: python python-3.x sharepoint upload onedrive


    【解决方案1】:

    我编写了您发布的示例代码。您将 auth_server_URL 替换为 Microsoft 帐户身份验证的身份验证 URL,该 URL 只能用于访问 OneDrive(消费产品)。您需要继续使用 login.microsoftonline.com URL 登录您的 OneDrive for Business 帐户。

    你是正确的,这会弹出一个对话框。但是,您可以编写一些支持代码,以便仅在您第一次登录特定应用程序时发生。请按照以下步骤操作(假设您使用的是AuthProvider 的默认实现:

    1. 通过auth.redeem_refresh_token() 行使用上面的示例代码
    2. AuthProvider 现在将有一个Session 对象,该对象缓存当前用户和会话的凭据。使用 AuthProvider.save_session() 保存凭据以供日后使用。
    3. 下次启动应用时,请使用AuthProvider.load_session()AuthProvider.refresh_token() 检索上一个会话并刷新身份验证令牌。这一切都将是无头的。

    请注意,SessionBase (found here) 的默认实现使用 Pickle,并且对于产品使用来说是不安全的。如果您打算将此应用部署给其他用户,请务必创建 Session 的新实现。

    【讨论】:

    • 所以它应该也适用于公司帐户?代码在这一行窒息,并永远留在这里:>>> auth_url = auth.get_auth_url(redirect_uri) 我在带有 python 2.7 的 Mac 上使用原始代码,除了 secret 和 app id 变量
    • 对不起,我为此道歉,代码停在code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri),下一行
    【解决方案2】:

    OneDrive 的网站在“OneDrive SDK for Python”到“OneDrive for Business”上显示“尚未” https://dev.onedrive.com/SDKs.htm

    github示例代码对我也不起作用,它试图弹出一个身份验证窗口,但IE找不到地址:

    http://('https//login.microsoftonline.com/common/oauth2/authorize',)?redirect_uri=http%3A%2F%2Flocalhost%3A8080&client_id=034xxxx9-9xx8-4xxf-bexx-1bc5xxxxbd0c&response_type=code

    或删除客户端 ID 中的所有“-”

    http://('https//login.microsoftonline.com/common/oauth2/authorize',)?redirect_uri=http%3A%2F%2Flocalhost%3A8080&client_id=034xxxx99xx84xxfbexx1bc5xxxxbd0c&response_type=code

    不管怎样,我得到了同样的结果,IE没有显示弹出窗口,有一行“无法显示此页面”

    【讨论】:

    • 2.5 年后,微软的 Graph API 似乎现在正在工作,我从他们基于 Lavarel 框架的示例中使用 PHP SDK 成功上传到 OneDrive for Business。 Microsoft Graph Starter project for PHP
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多