I’m trying to create a script to use the Cloud Build API. Using the website was no issue, I then moved onto terminal and again with CURL I can get a valid response.
However I’m having issues using the swagger generated API in python.
Minor point: I’m not sure I installed it correctly, as rather than having a “real” name after installing it, it’s called “swagger_client” - I’ve not used swagger before so it’s possible I did something incorrectly. I did try downloading some of the swagger demos and they all appear to use the same name…
Note: I’ve removed all identifying data and replaced it with {VALUE} eg. {API_KEY} - I am using correct values in my code, I just don’t want to share them on a public forum.
Once Installed I’ve setup a small script just to check things are working.
import swagger_client
from swagger_client import Configuration, ApiClient, BuildtargetsApi
# initialize the API client
configuration = Configuration();
configuration.username = {API_KEY};
client = ApiClient("https://build-api.cloud.unity3d.com/api/v1");
orgId = {orgId};
projectId = {projectId};
buildtargetsApi = BuildtargetsApi(client);
buildtargets = buildtargetsApi.get_build_targets(orgId, projectId);
I get an exception though:
HTTP response body: {“error”:“Not authorized. The server does not recognize the client.”}
Looking at the authorisation header used it is 64-bit encoding the username, and a colon (It’s expecting a username and password and my password is null at the moment)
def get_basic_auth_token(self):
"""
Gets HTTP basic authentication header (string).
:return: The token for basic HTTP authentication.
"""
return urllib3.util.make_headers(basic_auth=self.username + ':' + self.password)\
.get('authorization')
This appears to be causing the issue. I can manually make the request myself:
http = urllib3.PoolManager()
r = http.request('GET', 'https://build-api.cloud.unity3d.com/api/v1/orgs/{orgId}/projects/{projectId}/buildtargets', headers={'Authorization' : 'Basic {API_KEY}'})
r.data
This returns the correct value. But I get the “Not authorized” error for either of the following:
r = http.request('GET', 'https://build-api.cloud.unity3d.com/api/v1/orgs/{orgId}/projects/{projectId}/buildtargets', headers=urllib3.util.make_headers(basic_auth='{API_KEY}'))
r = http.request('GET', 'https://build-api.cloud.unity3d.com/api/v1/orgs/{orgId}/projects/{projectId}/buildtargets', headers=urllib3.util.make_headers(basic_auth='{API_KEY}:'))
Am I doing something wrong using the API? Does the API have a bug? Should the server be accepting 64-bit encoded authorisation requests?