def bauthenticator(self): from onedrivesdk.helpers.resource_discovery import ResourceDiscoveryRequest 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' http = onedrivesdk.HttpProvider() auth = onedrivesdk.AuthProvider(http, self.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, self.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] auth.redeem_refresh_token(service_info.service_resource_id) client = onedrivesdk.OneDriveClient( service_info.service_resource_id + '/_api/v2.0/', auth, http) return client
def init_business(client): """onedrivesdk.request.one_drive_client.OneDriveClient->onedrivesdk.request.one_drive_client.OneDriveClient Important: Only used for Business/Office 365! Init of the script. Let user login, get the details, save the details in a conf file. Used at the first time login. Ref: https://github.com/OneDrive/onedrive-sdk-python#onedrive-for-business https://dev.onedrive.com/auth/aad_oauth.htm#register-your-app-with-azure-active-directory """ # auth url: # https://login.microsoftonline.com/common/oauth2/authorize?scope=wl.signin+wl.offline_access+onedrive.readwrite&redirect_uri=https%3A%2F%2Fod.cnbeining.com&response_type=code&client_id=bac72a8b-77c8-4b76-8b8f-b7c65a239ce6 http = onedrivesdk.HttpProvider() auth = onedrivesdk.AuthProvider(http, client_id_business, auth_server_url=auth_server_url, auth_token_url=auth_token_url) auth_url = auth.get_auth_url(redirect_uri) # now the url looks like "('https://login.microsoftonline.com/common/oauth2/authorize',)?redirect_uri=https%3A%2F%2Fod.cnbeining.com&response_type=code&client_id=bac72a8b-77c8-4b76-8b8f-b7c65a239ce6" auth_url = auth_url.encode('utf-8').replace("('", '').replace("',)", '') # Ask for the code print('ATTENTION: This is for Onedrive Business and Office 365 only.') print('If you are using normal Onedrive, lease exit and run') print('') print('onedrivecmd init') print('') print(auth_url) print('') print('Paste this URL into your browser, approve the app\'s access.') print('Copy all the code in the new window, and paste it below:') code = input('Paste code here: ') auth.authenticate(code, redirect_uri, client_secret_business, resource='https://api.office.com/discovery/') # this step is slow 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) #print(client) return client
def authenticate(self, code): print('Athenticating...') self.auth_provider.authenticate(code, self.APP_REDIRECT_URL, self.APP_CLIENT_SECRET_BUSINESS, resource=self.APP_DISCOVERY_URL_BUSINESS) # this step can be slow service_info = ResourceDiscoveryRequest().get_service_info(self.auth_provider.access_token) self.APP_ENDPOINT = str(service_info[0]).split()[1] print('Refreshing token...') self.auth_provider.redeem_refresh_token(self.APP_ENDPOINT) print('Updating client') self.client = onedrivesdk.OneDriveClient(self.APP_ENDPOINT + '_api/v2.0/', self.auth_provider, self.http_provider) print('Authenticated!')
def Authentication(): http = sdk.HttpProvider() # sdk.HttpProvider() auth = sdk.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] auth.redeem_refresh_token(service_info.service_resource_id) client = sdk.OneDriveClient( service_info.service_resource_id + '/_api/v2.0/', auth, http)
def authenticate(self, cache, client_secret): assert cache auth_url = self.auth.get_auth_url(redirect_uri) code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri) self.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( self.auth.access_token)[0] self.auth.redeem_refresh_token(service_info.service_resource_id) print(service_info.service_resource_id) # Save the session for later self.auth.save_session(path=cache)
def login_business(self, redirect_uri, client_id, client_secret, discovery_uri, auth_server_url, auth_token_url): 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=discovery_uri) services = ResourceDiscoveryRequest().get_service_info( auth.access_token) for service in services: if service.service_id == 'O365_SHAREPOINT': auth.redeem_refresh_token(service.service_resource_id) client = onedrivesdk.OneDriveClient( service.service_resource_id + '/_api/v2.0/', auth, http) self.client = client return True return False
import onedrivesdk from onedrivesdk.helpers import GetAuthCodeServer from onedrivesdk.helpers.resource_discovery import ResourceDiscoveryRequest redirect_uri = 'http://localhost:8080' client_id = "2cffd357-331f-4a5e-a460-df4f4b39bbf2" client_secret = "muazSCTF033|tzyKKG87*?~" 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' 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=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] auth.redeem_refresh_token(service_info.service_resource_id) client = onedrivesdk.OneDriveClient( service_info.service_resource_id + '/_api/v2.0/', auth, http)