Exemplo n.º 1
0
    def login(self):
        result = dict(
            changed=False
        )

        if self.params['client_id'] is not None:
            try:
                self.conn = pysnow.OAuthClient(client_id=self.client_id,
                                               client_secret=self.client_secret,
                                               token_updater=self.updater,
                                               instance=self.instance)
            except Exception as detail:
                self.module.fail_json(msg='Could not connect to ServiceNow: {0}'.format(str(detail)), **result)
            if not self.session['token']:
                # No previous token exists, Generate new.
                try:
                    self.session['token'] = self.conn.generate_token(self.username, self.password)
                except pysnow.exceptions.TokenCreateError as detail:
                    self.module.fail_json(msg='Unable to generate a new token: {0}'.format(str(detail)), **result)

                self.conn.set_token(self.session['token'])
        elif self.username is not None:
            try:
                self.conn = pysnow.Client(instance=self.instance,
                                          user=self.username,
                                          password=self.password)
            except Exception as detail:
                self.module.fail_json(msg='Could not connect to ServiceNow: {0}'.format(str(detail)), **result)
        else:
            snow_error = "Must specify username/password. Also client_id/client_secret if using OAuth."
            self.module.fail_json(msg=snow_error, **result)
Exemplo n.º 2
0
def get_snow_client():
    global word_of_pass

    # Create the OAuthClient with the ServiceNow provided `client_id` and `client_secret`, and a `token_updater`
    # function which takes care of refreshing local token storage.
    client = pysnow.OAuthClient(client_id=client_ID,
                                client_secret=word_of_pass,
                                token_updater=updater,
                                instance=instance)

    if os.path.exists('token'):
        with open('token', 'r') as f:
            store['token'] = json.load(f)

    else:
        # No previous token exists. Generate new.
        username = raw_input("Enter Username: "******"Enter Password: ")
        store['token'] = client.generate_token(username, word_of_pass)
        # print (store['token'])
        with open('token', 'w') as f:
            f.write(json.dumps(store['token']))

    # Set the access / refresh tokens
    client.set_token(store['token'])

    return client
Exemplo n.º 3
0
 def updater(self, new_token):
     self.session['token'] = new_token
     self.conn = pysnow.OAuthClient(client_id=self.client_id,
                                    client_secret=self.client_secret,
                                    token_updater=self.updater,
                                    instance=self.instance)
     try:
         self.conn.set_token(self.session['token'])
     except pysnow.exceptions.MissingToken:
         snow_error = "Token is missing"
         self.module.fail_json(msg=snow_error)
     except Exception as detail:
         self.module.fail_json(msg='Could not refresh token: {0}'.format(str(detail)))
Exemplo n.º 4
0
 def _oauth_token_updater(self, new_token):
     self.token = new_token
     self.connection = pysnow.OAuthClient(
         client_id=self.client_id,
         client_secret=self.client_secret,
         token_updater=self._oauth_token_updater,
         instance=self.instance,
         host=self.host,
         raise_on_empty=self.raise_on_empty)
     try:
         self.connection.set_token(self.token)
     except pysnow.exceptions.MissingToken:
         self.module.fail(msg="Token is missing")
     except Exception as detail:
         self.module.fail(
             msg='Could not refresh token: {0}'.format(str(detail)))
Exemplo n.º 5
0
 def _auth_oauth(self):
     try:
         self.connection = pysnow.OAuthClient(
             client_id=self.client_id,
             client_secret=self.client_secret,
             token_updater=self._oauth_token_updater,
             instance=self.instance,
             host=self.host,
             raise_on_empty=self.raise_on_empty)
     except Exception as detail:
         self.fail(
             msg='Could not connect to ServiceNow: {0}'.format(str(detail)))
     if not self.token:
         # No previous token exists, Generate new.
         try:
             self.token = self.connection.generate_token(
                 self.username, self.password)
         except pysnow.exceptions.TokenCreateError as detail:
             self.fail(msg='Unable to generate a new token: {0}'.format(
                 str(detail)))
         self.connection.set_token(self.token)
Exemplo n.º 6
0
word_of_pass = '******'
store = {'token': None}
print('please do not run, duplicatation of data will occur')
exit(0)


# Takes care of refreshing the token storage if needed
def updater(new_token):
    print("OAuth token refreshed!")
    store['token'] = new_token


# Create the OAuthClient with the ServiceNow provided `client_id` and `client_secret`, and a `token_updater`
# function which takes care of refreshing local token storage.
client = pysnow.OAuthClient(client_id=client_ID,
                            client_secret=word_of_pass,
                            token_updater=updater,
                            instance=instance)

if os.path.exists('token'):
    with open('token', 'r') as f:
        store['token'] = json.load(f)

else:
    # No previous token exists. Generate new.
    username = raw_input("Enter Username: "******"Enter Password: ")
    store['token'] = client.generate_token(username, word_of_pass)
    print(store['token'])
    with open('token', 'w') as f:
        f.write(json.dumps(store['token']))
Exemplo n.º 7
0
                     str(sheet.cell(row=i, column=9).value) + ");\n\n")
    sql_work_note += work_note_sql

store = {'token': None}


# Takes care of refreshing the token storage if needed
def updater(new_token):
    print("OAuth token refreshed!")
    store['token'] = new_token


# Create the OAuthClient with the ServiceNow provided `client_id` and `client_secret`, and a `token_updater`
# function which takes care of refreshing local token storage.
s = pysnow.OAuthClient(client_id='eaf24769f03dc01cbf208c86789a4a5b',
                       client_secret='NP5vu|VawR',
                       token_updater=updater,
                       instance='sbcsnprod')

if not store['token']:
    #No previous token exists. Generate new.
    store['token'] = s.generate_token(
        'bcrsapi',
        '97!K}D%H$2@A7a{#rRE]@5H!v;9LM9Ht4_pXr3(aGc/&>wyErFQJ??Vc<9%QM<')

# Set the access / refresh tokens
s.set_token(store['token'])

with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(store, f, ensure_ascii=False, indent=4)

# Let's define a `Resource` for the incident API.