Currently I have a python function that takes the information of an Azure DevOps Service Connection. The problem is that I want to update that, and using a PUT request I always have a body problem. I´ve tried to use Postman to test if it works, and it is. The problem only remains on the python code. My code is this:
def update_sc_key(secret_value, app_name, sc_dependence):
print(f"SC DEPENDENCE:",sc_dependence)
personal_access_token = get_pat()
bearer = get_bearer()
if sc_dependence == True:
pat_credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=pat_credentials)
service_connection_name = app_name[len('SP_') + app_name[len('SP_'):].find('_') - 4:]
print(service_connection_name)
service_endpoint_client = connection.clients.get_service_endpoint_client()
service_connections = service_endpoint_client.get_service_endpoints(project_name)
for service_connection in service_connections:
if service_connection.name == "RENEWAL_SC_TEST": # service_connection_name
print(f'SERVICE CONNECTION NAME:', service_connection.name)
service_connection_id = service_connection.id
print(f'SERVICE CONNECTION ID:', service_connection_id)
# Get Service Connection body
project_url = organization_url + project_name + "/_apis/serviceendpoint/endpoints"
try:
# GET RESPONSE
get_params = {
"endpointIds": service_connection_id,
"api-version": "7.1-preview.4"
}
get_headers={
"Authorization": "Bearer " + bearer
}
get_response = requests.get(project_url, params=get_params, headers=get_headers)
get_response_tr = get_response.json()
get_data_obj1 = get_response_tr['value']
get_data_obj2 = str(get_data_obj1)
get_data_obj3 = get_data_obj2[1:-1]
if get_response.status_code == 200:
get_data_obj_mod = get_data_obj3.replace("'authenticationType': 'spnKey'", "'authenticationType': 'spnKey', 'serviceprincipalkey': '" + secret_value + "'")
get_data_obj_mod_rep = get_data_obj_mod.replace("'", "\"").replace("True", "true").replace("False", "false")
get_data_obj_mod_rep = json.load(get_data_obj_mod)
print(get_data_obj_mod_rep)
else:
print(f"Error: {get_response.status_code} - {get_response.text}")
# PUT RESPONSE TO CHANGE PWD
put_params = {
"endpointId": service_connection_id,
"api-version": "7.1-preview.4"
}
put_headers={
"Authorization": "Bearer " + bearer,
"Content-Type": "application/json"
}
put_body={
"body": get_data_obj_mod_rep
}
put_response = requests.put(project_url, params=put_params, headers=put_headers, data=put_body)
print(put_response.status_code)
print(put_response.text)
except json.JSONDecodeError as e:
print("ERROR: expired bearer token")
else:
print(f'No Service Connection')
The result of the variable "get_data_obj_mod_rep" works on Postman as I said, but if I use the same on python, I get this error:
{"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: endpoint","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
Based on your python sample, I can reproduce the similar issue.
To solve this issue, I change to use another python format to get the service connection body and modify it.
Here is an example:
In this case, I can update the service connection successfully.
Update:
Update1:
When we use the params as the sample you shared, the url will change to below format:
This is not correct.
The correct format:
This should be the root cause of the issue.
The following format will work too:
Update3: