def setUp(self): self.client_id = os.environ.get('CLIENT_ID') self.client_secret = os.environ.get('CLIENT_SECRET') self.token = os.environ.get('ACCESS_TOKEN') self.redirect_url = os.environ.get('REDIRECT_URL') self.resource = os.environ.get('RESOURCE') self.client = Client(client_id=self.client_id, client_secret=self.client_secret, token=self.token)
def login(args): client_id = args.client_id #From Azure app registration client_secret = args.client_secret #From Azure app secrets redirect_url = 'https://stavrostest/auth' client = Client(args.resource, client_id, client_secret) #https://github.com/GearPlug/dynamics365crm-python #You construct an authorization url using the url_petition method #Here you must set the redirect URL that you have also set in the Azure app url = client.url_petition(redirect_url) #The user access to this url and authorize your app with a set of permissions (scopes). #The user is redirected to your web server and you grab the authorization code and exchange it # with the exchange_code method for an access token. # Then you start using the provided access token to access resources on the user behalf. webbrowser.open(url) #Get the code from URL, it can be used only once to get a token code = input("Enter the code you've got from the URL: ") token = client.exchange_code(redirect_url, code) client.set_token(token['access_token']) return client
class Dynamics365CRMTestCases(TestCase): def setUp(self): self.client_id = os.environ.get('CLIENT_ID') self.client_secret = os.environ.get('CLIENT_SECRET') self.token = os.environ.get('ACCESS_TOKEN') self.redirect_url = os.environ.get('REDIRECT_URL') self.resource = os.environ.get('RESOURCE') self.client = Client(client_id=self.client_id, client_secret=self.client_secret, token=self.token) def test_oauth_access(self): url = self.client.url_petition(self.redirect_url, self.resource) self.assertIsInstance(url, str) o = urlparse(url) query = parse_qs(o.query) self.assertIn('client_id', query) self.assertEqual(query['client_id'][0], self.client_id) self.assertIn('redirect_uri', query) self.assertEqual(query['redirect_uri'][0], self.redirect_url) def test_get_data(self): response = self.client.get_data( type="contacts", select="fullname, emailaddress1, createdon", orderby="createdon desc", top="1") self.assertIsInstance(response, dict) def test_create_data(self): response = self.client.create_data(type='contacts', firstname="NAME", lastname="LASTNAME", middlename="MIDDLENAME", emailaddress1="*****@*****.**") print(response) self.assertIsInstance(response, bool)
def UserSaidYes(text): while True: response = input(text + " (y/n) ") if response in ["Y", "y"]: return True elif response in ["N", "n"]: return False else: print("Sorry, didn't get that") resource = input("Dynamics 365 URL? ") token = dynamics365ce_helpers.GetInteractiveToken(resource) client = Client(resource, token=token) matricula = input("matricula?") while True: if UserSaidYes("Update?"): for x in client.get_data(type="account")['value']: if x['axx_matricula'] == matricula: print("Old values:", x['accountid'], x['axx_matricula'], x['axx_nombreapellido'], x['statecode'], x['statuscode'], x['address1_postalcode']) client.update_lead(x['accountid'], statuscode=1, address1_postalcode='666') break for x in client.get_data(type="account")['value']:
#The user access to this url and authorize your app with a set of permissions (scopes). #The user is redirected to your web server and you grab the authorization code and exchange it # with the exchange_code method for an access token. # Then you start using the provided access token to access resources on the user behalf. webbrowser.open(url) #Get the code from URL, it can be used only once to get a token code = input("Enter the code you've got from the URL: ") token = client.exchange_code(redirect_url, code) client.set_token(token['access_token']) return client args = readConfig() tokenFile = "token.txt" if os.path.isfile(tokenFile): f = open(tokenFile, "r") token = f.read() f.close() client = Client(resource=args.resource, token=token) else: client = login(args) f = open(tokenFile, "w") f.write(client.token) f.close() list_contacts = client.get_contacts() print(list_contacts)
from dynamics365crm.client import Client import dynamics365ce_helpers def Query(entity, fields): records = client.get_data(type=entity) for r in records['value']: for f in fields: print(r[f], sep=' ', end='') print() resource = input("Dynamics 365 URL? ") token = dynamics365ce_helpers.GetInteractiveToken(resource = resource) client = Client(resource, token=token) Query("privileges", ['name', 'accessright'])
def UserSaidYes(text): while True: response = input(text + " (y/n) ") if response in ["Y", "y"]: return True elif response in ["N", "n"]: return False else: print("Sorry, didn't get that") resource = input("Dynamics 365 URL? ") token = dynamics365ce_helpers.GetInteractiveToken(resource) client = Client(resource, token=token) fullname = input("fullname?") while True: if UserSaidYes("Update?"): for x in client.get_data(type="leads")['value']: if x['fullname'] == fullname: print("Old values:", x['leadid'], x['fullname'], x['statecode'], x['statuscode'], x['axx_aprobacionabm']) client.update_lead(x['leadid'], statuscode = 1, axx_aprobacionabm = 282270001) break for x in client.get_data(type="leads")['value']: if x['fullname'] == fullname: print("Values now:", x['leadid'], x['fullname'], x['statecode'], x['statuscode'], x['axx_aprobacionabm']) break
pd.set_option('precision', 0) RESOURCE_URI = 'RESOURCE URL' # O365 credentials for authentication w/o login prompt USERNAME = '******' PASSWORD = '******' # Azure Directory OAUTH 2.0 AUTHORIZATION ENDPOINT AUTHORIZATION_URL = 'AUTH URL' token_response = adal.acquire_token_with_username_password( AUTHORIZATION_URL, USERNAME, PASSWORD, resource=RESOURCE_URI) token = token_response['accessToken'] refresh = token_response['refreshToken'] client = Client(RESOURCE_URI, token) stoken = client.set_token(token) get_accounts = client.get_accounts() accounts = get_accounts['value'] df = pd.DataFrame(accounts) df = df[df['new_nameindy'].notnull()] columns = list(df.columns) df1 = df[[ 'accountid', 'name', 'new_nameindy', 'new_lastrfpreceived', 'new_receivedthisuwyear', 'new_alltimereceived', 'new_lastquoteddate', 'new_quotedthisuwyear', 'new_alltimequoted', 'new_lastsolddate', 'new_soldthisuwyear', 'new_alltimesold', 'new_firstquoteddate',
import os import json from urllib.parse import urlparse, parse_qs from dynamics365crm.client import Client import GetD365Token resource = input("Dynamics 365 URL? ") token = GetD365Token.GetInteractiveToken(resource = resource) client = Client(resource, token=token) contacts = client.get_data(type="contacts") for x in contacts['value']: print(x['fullname'])
from adal import AuthenticationContext from dynamics365crm.client import Client DYNAMIC_RESOURCE = 'https://edge.crm2.dynamics.com/' auth_context = AuthenticationContext( 'https://login.microsoftonline.com/76b77ddd-7462-4810-b24e-bc1668ab6f19') token = auth_context.acquire_token_with_username_password( 'https://edge.crm2.dynamics.com/', '*****@*****.**', 'Murilao23', '87dafbc3-e1eb-418f-abbb-3d526d1508d4') #'e84e50cb-b9df-4311-b672-b47951e6a2c7') client = Client(DYNAMIC_RESOURCE) client.set_token(token["accessToken"])
def UserSaidYes(text): while True: response = input(text + " (y/n) ") if response in ["Y", "y"]: return True elif response in ["N", "n"]: return False else: print("Sorry, didn't get that") resource = input("Dynamics 365 URL? ") token = dynamics365ce_helpers.GetInteractiveToken(resource) client = Client(resource, token=token) fullname = input("fullname?") while True: if UserSaidYes("Update?"): for x in client.get_data(type="leads")['value']: if x['fullname'] == fullname: print("Old values:", x['leadid'], x['fullname'], x['statecode'], x['statuscode'], x['axx_estadodesolicitudaltacrm'], x['axx_mensajedesolicitudaltacrm'], x['axx_codigonuevoclientealtacrm']) client.update_lead(x['leadid'], axx_estadodesolicitudaltacrm='66', axx_mensajedesolicitudaltacrm= 'De nuevo ha ocurrido un error indeseado',