title: Update the Azure DevOps service endpoint (connection) using REST API
body: <p>we are using the REST API Method (<a href="https://docs.microsoft.com/en-us/rest/api/azure/devops/serviceendpoint/endpoints/update%20service%20endpoint?view=azure-devops-rest-6.0" rel="nofollow noreferrer">PUT</a>) to update the existing AWS service connection in our ADO environment by assigning a minimum level of access (scopes) to the PAT.</p>
  <p>The access levels are</p>
  <ol>
  <li>Service Connections (Read, query, and manage)</li>
  <li>Tokens (Read, update, and revoke)</li>
  </ol>
  <p>There are no issues with this until past few days. We were able update the service connection on a scheduled basis using Azure Pipelines. But since past few days it started throwing 401 Unauthorized error. but it was working fine before. Not sure if they (Azure) have changed something in the latest release.</p>
  <p>I tried to troubleshoot the issue by increasing the level of access scope one by one (assigned all the scopes at some point), but no luck with it. However, I was able to update the service connection, If I change the level of access to a full access instead of custom defined. But it is not a good idea to give the full level of access to a personal access token.</p>
  <p>Am I missing something here or Is there a way to update the service connection with a minimum level of access control?</p>
  <p>Also, what is difference between full access vs custom defined with all the scopes, since the full access is working fine, but custom defined with all the scopes is not working.</p>
  <p>below is the snippet of code for your reference</p>
  <pre><code>$OrganizationName = &quot;&quot; 
  $ProjectName = &quot;&quot;
  $PAT = &quot;&quot;
  $AccessKeyID = &quot;&quot;
  $SecretAccessKey = &quot;&quot; 
  $Serviceconnectionname = &quot;&quot;
  
  
  function Update-AWSServiceConnection {
  
      Write-Host &quot;Executing the Update Service Connection Script..&quot;
  
      # Create the header to authenticate to Azure DevOps
      Write-Host &quot;Create the header to authenticate to Azure DevOps&quot;
      $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(&quot;:$($PAT)&quot;))
  
      $Headers = @{
          Authorization = &quot;Basic $token&quot;
      }
  
      # Get the Project ID
      Write-Host &quot;Construct the API URL to get the project ID..&quot;
      $project = &quot;https://dev.azure.com/&quot; + &quot;$OrganizationName/_apis/projects/$ProjectName ?api-version=6.0&quot;
      Write-Host &quot;Project API URL :: $project&quot;
      try {
          Write-Host &quot;Get the Project [$ProjectName] ID..&quot;
          $response = Invoke-RestMethod -Uri $project -Headers $Headers -Method GET
          $ProjectID = $response.id
          if (!$ProjectID) {
              Write-Host &quot;ProjectID value is null&quot;
              Write-Error &quot;Script step has been aborted.&quot; -ErrorAction stop
          } else {
              Write-Host &quot;Project ID :: $ProjectID&quot;
          }
      }
      catch {
          $ErrorMessage = $_ | ConvertFrom-Json
          throw &quot;Could not Get the project [$ProjectName] ID: $($ErrorMessage.message)&quot;
      }
  
      # Get Endpoint ID Details
      $endpoint = &quot;https://dev.azure.com/&quot; + &quot;$OrganizationName/$ProjectName/_apis/serviceendpoint/endpoints?endpointNames=$Serviceconnectionname&amp;api-version=6.0-preview.4&quot;
  
      try {
          Write-Host &quot;Get the Service Connection [$Serviceconnectionname] ID..&quot;
          $response = Invoke-RestMethod -Uri $endpoint -Headers $Headers -Method GET
          $endpointId = $response.value.id
          if (!$endpointId) {
              Write-Host &quot;Service Endpoint ID value is null&quot;
              Write-Error &quot;Script step has been aborted.&quot; -ErrorAction stop
          } else {
              Write-Host &quot;Service Endpoint ID :: $endpointId&quot;
          }
  
      }
      catch {
          $ErrorMessage = $_ | ConvertFrom-Json
          throw &quot;Could not Get the service connection [$Serviceconnectionname] ID: $($ErrorMessage.message)&quot;
      }
  
      # Create a body for the API call
      $url = &quot;https://dev.azure.com/&quot; + &quot;$OrganizationName/_apis/serviceendpoint/endpoints/$endpointId ?api-version=6.1-preview.4&quot;
      $body = @&quot;
  {
      &quot;data&quot;: {},
      &quot;id&quot;: &quot;$endpointId&quot;,
      &quot;name&quot;: &quot;UpdatedServiceEndpoint&quot;,
      &quot;type&quot;: &quot;AWS&quot;,
      &quot;url&quot;: &quot;https://aws.amazon.com/&quot;,
      &quot;description&quot;: null,
      &quot;authorization&quot;: {
        &quot;parameters&quot;: {
          &quot;username&quot;: &quot;$AccessKeyId&quot;,
          &quot;password&quot;: &quot;$SecretAccessKey&quot;
          },
          &quot;scheme&quot;: &quot;UsernamePassword&quot;,
      },
      &quot;isShared&quot;: false,
      &quot;isReady&quot;: true,
      &quot;owner&quot;: &quot;Library&quot;,
      &quot;serviceEndpointProjectReferences&quot;: [
        {
          &quot;name&quot;: &quot;$Serviceconnectionname&quot;,
          &quot;projectReference&quot;: {
            &quot;id&quot;: &quot;$ProjectID&quot;,
            &quot;name&quot;: &quot;$ProjectName&quot;
          }
        }
      ]
    }
  &quot;@
  
      try { 
      Write-Host &quot;Updating the Service Connection [$Serviceconnectionname]&quot;
      $response = Invoke-RestMethod -Uri $url -Headers $Headers -Method PUT -Body $body -ContentType application/json
      Write-Host &quot;Connection Updated&quot;
      $response
      }
      catch {
        Write-Host &quot;An error occurred:&quot;
        Write-Host $_
      }
      
  }
  
  
  Update-AWSServiceConnection 
  </code></pre>
  <p>Any suggestions or comments would be greatly appreciated.</p>