2

I'm working with bitbucket pipelines to build and upload docker images automatically. We use AWS for our docker repo and they change their login credentials every 12 hours, so we need to retrieve and update the environment variables being used by bitbucket so that it can push the images to aws. I've written the following code using python requests to try to update the environment variables for docker user and password but am getting a "500 Internal Server Error" from bitbucket.

def update_pipeline_variable(uuid, name, value, token):

    endpoint = 'https://api.bitbucket.com/2.0/repositories/myuser/myrepo/pipelines_config/variables/{{{variable_uuid}}}?access_token={token}'.format(
        variable_uuid=uuid,
        token=token
    )

    body = {
        'value': value,
        'key': name
    }

    headers = {"Authorization": "Bearer {token}".format(token=token)}

    r = requests.put(endpoint, json=body, headers=headers)

    if r.status_code == 200:
        print('Successfully updated {variable_name} environment variable.'.format(variable_name=name))
        return None

    raise RuntimeError('The following error occurred while updating the environment: {error}'.format(error=r.content))

If I remove the access token from the request url and keep it only in the auth header, then I get a 404 instead of a 500. I know the tokens and uuids are valid because I am able to make the request successfully using Postman, so it's something specific to my python implementation. I also tried using the oauth2 requests library with exactly the same results.

smitty1138
  • 21
  • 1
  • Can you check `r.url` and see if it contains anything extraneous like extra `{` and `}` characters? I suspect that your `{{{variable_uuid}}}` portion of the URL is causing problems. – wkl Oct 02 '17 at 18:47
  • The url correctly wraps the uuid in a single set of {}. I've actually copy and pasted the url that the request uses into Postman to verify and it works... – smitty1138 Oct 03 '17 at 16:23
  • If you still need help for that, you can try the request again with encoded `{` and `}`. `{` would be `%7B` and `}` would be `%7D`. In general the `requests` module in python does not encode on its own, on the other hand Postman does it. Therefore, always be sure that you encode variables which are inserted into urls in python. – TheFRedFox Aug 02 '19 at 12:48

0 Answers0