Python SDK - Add proxy support for auth

The latest version of the API SDK (60.0) includes Proxy and Client Credential settings. However the proxy is only used for the calls once the token is set, not for the actual client credentials helper. Could you please add it?
(or correct me if I am wrong)

I've created an issue to investigate this.

Thank you Tim. Please keep us updated on the forum.

I'm looking into the code and as long as the proxy has been set when the ApiClient class is initialized, it should use it for both the auth request and all subsequent requests. Both the get_client_credentials_token and __call_api methods use the self.request method to make API requests. Are you possibly using different instances of ApiClient in your code?

Thanks Tim. Then I am probably doing something wrong:

PureCloudPlatformClientV2.configuration.host = 'https://api.mypurecloud.jp'
PureCloudPlatformClientV2.configuration.proxy = PROXYURL
apiclient = PureCloudPlatformClientV2.api_client.ApiClient().get_client_credentials_token(id,secret)
authApi = PureCloudPlatformClientV2.AuthorizationApi(apiclient)

Those doesn't work because the configuration setup in the first 2 lines are not used in the get_client_credentials_token.
How do you pass the configuration values to the ApiClient? It is not part of the arguments for the constructor.

The rest client gets the host and proxy from the configuration when it's instantiated. I don't currently have access to an appropriate proxy to test this, but I'll see if I can get one set up. The code you shared should be using the proxy for both requests as far as I can tell.

I'm not able to reproduce this issue. I have a proxy running on localhost:443 and running this program exactly as-is, only using a valid client id and secret, makes a request to the configured proxy as expected.

import PureCloudPlatformClientV2
PureCloudPlatformClientV2.configuration.host = "https://api.mypurecloud.com"
PureCloudPlatformClientV2.configuration.proxy = "https://localhost"
apiclient = PureCloudPlatformClientV2.api_client.ApiClient().get_client_credentials_token("xxx", "xxx")

Hi Tim,
Here are my own results:
Code:

clientId = 'XXX'
clientSecret = 'XXX'
PureCloudPlatformClientV2.configuration.host = 'https://api.mypurecloud.jp'
PureCloudPlatformClientV2.configuration.proxy = PROXYURL
apiclient = PureCloudPlatformClientV2.api_client.ApiClient().get_client_credentials_token(clientId, clientSecret)

And the error stack (I added a print to know which url is used):

https://login.mypurecloud.com/oauth/token
2019-10-11 09:56:54,702 WARNING Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required'))': /oauth/token
2019-10-11 09:56:54,897 WARNING Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required'))': /oauth/token
2019-10-11 09:56:55,102 WARNING Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required'))': /oauth/token
Traceback (most recent call last):
File "C:\Users\M.Poulain\eclipse-workspace\PureCloudSDK\recordings.py", line 190, in
Recordings(iat=0).run()
File "C:\Users\M.Poulain\eclipse-workspace\PureCloudSDK\recordings.py", line 33, in init
apiclient = PureCloudPlatformClientV2.api_client.ApiClient().get_client_credentials_token(clientId, clientSecret)
File "C:\Python37\lib\site-packages\PureCloudPlatformClientV2\api_client.py", line 120, in get_client_credentials_token
post_params=post_params, body=body);
File "C:\Python37\lib\site-packages\PureCloudPlatformClientV2\api_client.py", line 437, in request
body=body)
File "C:\Python37\lib\site-packages\PureCloudPlatformClientV2\rest.py", line 222, in POST
body=body)
File "C:\Python37\lib\site-packages\PureCloudPlatformClientV2\rest.py", line 190, in request
raise ApiException(http_resp=r)
PureCloudPlatformClientV2.rest.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Date': 'Fri, 11 Oct 2019 00:56:56 GMT', 'Inin-Correlation-Id': '92ddd4b4-bbca-4b5b-5b2c-ebc78b85d8f3', 'Strict-Transport-Security': 'max-age=7776000', 'Vary': 'Accept-Encoding', 'Content-Length': '65', 'Connection': 'keep-alive'})
HTTP response body: {"error":"invalid_client","description":"authentication failed"}

It seems that my request was sent without proxy (after 3 failed requests with Proxy), but to the USA host (default), not Japan.
So both configuration options are not taken into account by the client credentials request.

Ah, so it is using the proxy but your proxy isn't working and the fallback behavior isn't as expected. That's a very different issue. I'll see if I can reproduce this behavior.

I've released a fix for the issue in version 64.0.1. The SDK was using the proxy and host that are configured. The issue was isolated to the logic in determining the hostname for the auth service based on the configured hostname. It was incorrectly forcing .com on the hostname, which is why I didn't notice originally because both environments I tested with ended with .com.

Thanks Tim. I confirm it works now.
I still have issues with my proxy but I am fearing it is linked to my local environment.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.