|
32 | 32 | import random |
33 | 33 | import tempfile |
34 | 34 | import threading |
| 35 | +import base64 |
| 36 | +import json |
| 37 | + |
35 | 38 |
|
36 | 39 | from datetime import datetime |
37 | 40 | from datetime import date |
@@ -87,6 +90,74 @@ def __init__(self, host=None, header_name=None, header_value=None, cookie=None): |
87 | 90 | self.access_token = "" |
88 | 91 |
|
89 | 92 |
|
| 93 | + |
| 94 | + def get_client_credentials_token(self, client_id, client_secret): |
| 95 | + """ |
| 96 | + :param client_id: Client ID to authenticate with |
| 97 | + :param client_secret: Client Secret to Authenticate with |
| 98 | + :return: Returns the Api Object which allows this to directly enter into an api call, also sets the token |
| 99 | + """ |
| 100 | + query_params = {} |
| 101 | + body = None |
| 102 | + host = (self.host).split('.', 2)[1] |
| 103 | + url = 'https://login.' + host + '.com/oauth/token' |
| 104 | + |
| 105 | + post_params = {'grant_type': 'client_credentials'} |
| 106 | + |
| 107 | + auth_string = 'Basic ' + base64.b64encode(bytes((client_id + ':' + client_secret).encode('ascii'))).decode( |
| 108 | + 'ascii') |
| 109 | + |
| 110 | + header_params = { |
| 111 | + "Authorization": auth_string, |
| 112 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 113 | + } |
| 114 | + header_params = self.sanitize_for_serialization(header_params) |
| 115 | + post_params = self.sanitize_for_serialization(post_params) |
| 116 | + |
| 117 | + response = self.request("POST", url, |
| 118 | + query_params=query_params, |
| 119 | + headers=header_params, |
| 120 | + post_params=post_params, body=body); |
| 121 | + data = json.loads('[' + response.data + ']') |
| 122 | + self.access_token = data[0]["access_token"] |
| 123 | + return self; |
| 124 | + |
| 125 | + def get_saml2bearer_token(self,client_id,client_secret,org_name,assertion): |
| 126 | + """:param client_id: Client Id to authenticate with |
| 127 | + :param client_secret: Client Secret to authenticate with |
| 128 | + :param org_name: Orginization name to authenticate with |
| 129 | + :param assertion: SamlAssertion encoded |
| 130 | + :return: |
| 131 | + """ |
| 132 | + |
| 133 | + query_params = {} |
| 134 | + body = None |
| 135 | + host = (self.host).split('.', 2)[1] |
| 136 | + url = 'https://login.' + host + '.com/oauth/token' |
| 137 | + |
| 138 | + post_params = {'grant_type': 'urn:ietf:params:oauth:grant-type:saml2-bearer', |
| 139 | + 'orgName': org_name, |
| 140 | + 'assertion': assertion |
| 141 | + } |
| 142 | + |
| 143 | + auth_string = 'Basic ' + base64.b64encode(bytes((client_id + ':' + client_secret).encode('ascii'))).decode( |
| 144 | + 'ascii') |
| 145 | + |
| 146 | + header_params = { |
| 147 | + "Authorization": auth_string, |
| 148 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 149 | + } |
| 150 | + header_params = self.sanitize_for_serialization(header_params) |
| 151 | + post_params = self.sanitize_for_serialization(post_params) |
| 152 | + |
| 153 | + response = self.request("POST", url, |
| 154 | + query_params=query_params, |
| 155 | + headers=header_params, |
| 156 | + post_params=post_params, body=body); |
| 157 | + data = json.loads('[' + response.data + ']') |
| 158 | + self.access_token = data[0]["access_token"] |
| 159 | + return self; |
| 160 | + |
90 | 161 | @property |
91 | 162 | def user_agent(self): |
92 | 163 | """ |
@@ -116,7 +187,7 @@ def __call_api(self, resource_path, method, |
116 | 187 | header_params['Cookie'] = self.cookie |
117 | 188 | if header_params: |
118 | 189 | header_params = self.sanitize_for_serialization(header_params) |
119 | | - header_params['purecloud-sdk'] = '57.0.0' |
| 190 | + header_params['purecloud-sdk'] = '57.0.1' |
120 | 191 |
|
121 | 192 | # path parameters |
122 | 193 | if path_params: |
|
0 commit comments