1

I'm trying to upload a file to OneDrive of an MS SharePoint using Python API. I've succeeded in authentication, but I can't get any service_info returned.

I've already looked here and here. I'm experiencing the exact same issues describe there. I've modified my code accordingly, but nothing seems to work.

Here's my complete code.

import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer
from onedrivesdk.helpers.resource_discovery import ResourceDiscoveryRequest, ServiceInfo
from onedrivesdk.helpers import http_provider_with_proxy

redirect_uri = 'http://localhost:8080'
client_id='xxxxxxx'
client_secret = 'xxxxxxx'
discovery_uri = 'https://api.office.com/discovery/'
auth_server_url='https://login.microsoftonline.com/common/oauth2/authorize'
auth_token_url='https://login.microsoftonline.com/common/oauth2/token'
proxy = {
    'http': 'xxx',
    'https': 'xxx'
}

import json
import requests
class AnyVersionResourceDiscoveryRequest(ResourceDiscoveryRequest):

    def get_all_service_info(self, access_token, sharepoint_base_url):
        headers = {'Authorization': 'Bearer ' + access_token}        
        response = json.loads(requests.get(self._discovery_service_url,
                                           headers=headers).text)
        service_info_list = [ServiceInfo(x) for x in response['value']]
        # Get all services, not just the ones with service_api_version 'v2.0'
        # Filter only on service_resource_id
        sharepoint_services = \
            [si for si in service_info_list
             if si.service_resource_id == sharepoint_base_url]
        return sharepoint_services

sharepoint_base_url = 'https://xxx.sharepoint.com/'

# http = onedrivesdk.HttpProvider()
http = http_provider_with_proxy.HttpProviderWithProxy(proxy, verify_ssl=True)
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=discovery_uri)
# 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]
service_info = AnyVersionResourceDiscoveryRequest().get_all_service_info(auth.access_token, sharepoint_base_url)[0]
auth.redeem_refresh_token(service_info.service_resource_id)
client = onedrivesdk.OneDriveClient(service_info.service_resource_id + '/_api/v2.0/', auth, http)

I'm stuck at the 3rd line from the bottom. What am I doing wrong here?

Community
  • 1
  • 1
Onur Ece
  • 105
  • 1
  • 6

0 Answers0