Search Unity

Cloud Build Api "Not authorized. The server does not recognize the client."

Discussion in 'Unity Build Automation' started by Bailywick, Aug 24, 2017.

  1. Bailywick

    Bailywick

    Joined:
    Mar 11, 2015
    Posts:
    17
    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.
    Code (csharp):
    1. import swagger_client
    2. from swagger_client import Configuration, ApiClient, BuildtargetsApi
    3.  
    4. # initialize the API client
    5. configuration = Configuration();
    6. configuration.username = {API_KEY};
    7. client = ApiClient("https://build-api.cloud.unity3d.com/api/v1");
    8.  
    9. orgId = {orgId};
    10. projectId = {projectId};
    11.  
    12. buildtargetsApi = BuildtargetsApi(client);
    13. 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)
    Code (csharp):
    1. def get_basic_auth_token(self):
    2.         """
    3.        Gets HTTP basic authentication header (string).
    4.  
    5.        :return: The token for basic HTTP authentication.
    6.        """
    7.         return urllib3.util.make_headers(basic_auth=self.username + ':' + self.password)\
    8.                            .get('authorization')
    This appears to be causing the issue. I can manually make the request myself:
    Code (csharp):
    1. http = urllib3.PoolManager()
    2. r = http.request('GET', 'https://build-api.cloud.unity3d.com/api/v1/orgs/{orgId}/projects/{projectId}/buildtargets', headers={'Authorization' : 'Basic {API_KEY}'})
    3. r.data
    This returns the correct value. But I get the "Not authorized" error for either of the following:
    Code (csharp):
    1. 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}'))
    2. 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?
     
    ModLunar likes this.
  2. Bailywick

    Bailywick

    Joined:
    Mar 11, 2015
    Posts:
    17
    Is this maybe the wrong place to post questions about the API?

    For anyone else with similar issues in the future my solution has been to create my own cut down python API library for the bits I need based on the bash API examples using the libs: certifi, urllib3, json, six.
     
    Last edited: Sep 5, 2017
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    were you able to make it work eventually, i am hitting the same issues with it?
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    actually managed to make the python api work, if you find the auth_settings function in configuration.py, just change the permissions section to be the same as the apikey section
     
    mpeddicord and Bailywick like this.
  5. mpeddicord

    mpeddicord

    Joined:
    Mar 17, 2013
    Posts:
    13
    You are an excellent human being. Thank you.