2016-08-26 45 views
0

任何人都知道這是否可能? 我只想自動將一些文檔放入我的onedrive for business帳戶。python3上傳文件到ondrive或sharepoint?

我試圖

   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,並有一個祕密和ID。但是當我跑這個我得到範圍是無效的錯誤。另外它會嘗試啓動一個不適合命令行環境的網頁。我認爲這個SDK可能也過時了,因爲最初這個腳本有login.microsoftonline,但是這是無法訪問的,所以我將它改爲login.live.com。

回答

0

我寫了你發佈的這個示例代碼。您將auth_server_URL替換爲Microsoft帳戶身份驗證的身份驗證URL,該身份驗證URL只能用於訪問OneDrive(消費者產品)。您需要繼續使用login.microsoftonline.com網址登錄到您的OneDrive for Business帳戶。

你是對的,這會彈出一個對話框。但是,您可以編寫一些支持代碼,以便僅在您第一次登錄到特定應用程序時纔會發生。按照以下步驟(假設你使用的AuthProvider默認實現:

  1. 通過線上使用上面的示例代碼了auth.redeem_refresh_token()
  2. AuthProvider現在將有一個Session對象,緩存當前用戶的憑據和會話,使用AuthProvider.save_session()保存憑據後。
  3. 下一次啓動您的應用程序,使用AuthProvider.load_session()AuthProvider.refresh_token()找回以前的會話並刷新身份驗證令牌,這都將是無頭。

請注意,SessionBasefound here)的默認實現使用Pickle,因此不適合產品使用。如果您打算將此應用程序部署到其他用戶,請務必創建Session的新實現。

+0

所以它應該是爲企業工作賬戶呢?在這條線上的代碼扼流圈,並永遠留在這裏: '>>> auth_url = auth.get_auth_url(redirect_uri)' 我在Mac上使用原始代碼,除了祕密和應用程序id變量, python 2.7 –

+0

我的不好,我很抱歉,代碼停在'code = GetAuthCodeServer.get_auth_code(auth_url,redirect_uri)',下一行 –

0

Onerive的網站顯示「尚未」的「OneDrive SDK的Python」到「OneDrive商務」 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 =代碼

無論哪種方式,我得到了相同的結果,即沒有表現出與線彈出「此頁無法顯示」