python3上传文件到ondrive或sharepoint?

时间:2016-08-26 23:25:24

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

任何人都知道这是否可行? 我只想自动将一些文档放入我的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并获得了一个秘密和身份证明。但是当我运行这个时,我得到的范围是无效错误。此外,它试图启动一个不适合命令行类型环境的网页。我认为这个SDK也可能已经过时了,因为最初这个脚本有login.microsoftonline,但是无法访问,所以我将其更改为login.live.com。

2 个答案:

答案 0 :(得分:0)

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

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

  1. 通过第auth.redeem_refresh_token()
  2. 使用上面的示例代码
  3. AuthProvider现在将有一个Session对象,用于缓存当前用户和会话的凭据。使用AuthProvider.save_session()保存凭据以供日后使用。
  4. 下次启动应用时,请使用AuthProvider.load_session()AuthProvider.refresh_token()检索上一个会话并刷新身份验证令牌。这将是无头的。
  5. 请注意SessionBasefound here)的默认实施使用Pickle,对产品使用不安全。如果您打算将此应用程序部署到其他用户,请务必创建Session的新实现。

答案 1 :(得分:0)

Onerive的网站将“OneDrive SDK for Python”中的“Not Yet”显示为“OneDrive for Business” https://dev.onedrive.com/SDKs.htm

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

的http://( '// HTTPS login.microsoftonline.com /普通/的oauth2 /授权',)REDIRECT_URI = HTTP%3A%2F%2Flocalhost%3A8080&安培; CLIENT_ID = 034xxxx9-9xx8-4xxf-bexx-1bc5xxxxbd0c&安培; RESPONSE_TYPE =代码

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

的http://( '// HTTPS login.microsoftonline.com /普通/的oauth2 /授权',)REDIRECT_URI = HTTP%3A%2F%2Flocalhost%3A8080&安培; CLIENT_ID = 034xxxx99xx84xxfbexx1bc5xxxxbd0c&安培; RESPONSE_TYPE =代码

无论哪种方式,我得到了相同的结果,IE没有显示弹出一行“此页面无法显示”