title: Azure Devops Rest API Create Personal Access Token(PAT) -- patTokenError : userIdRequired
body: <p>I am able to list existing PATs when I use the GET verb.</p>
  <p>I get a 200 OK response when I create a PAT using the POST verb.</p>
  <p>The payload shows:</p>
  <p><code>{&quot;patToken&quot;:null,&quot;patTokenError&quot;:&quot;userIdRequired&quot;}</code></p>
  <p>This error is listed in the documentation but does not explain how to resolve it.</p>
  <pre><code>var clientBearer = new RestClient(@&quot;https://login.microsoftonline.com&quot;);
  var requestBearer = new RestRequest($&quot;{tenantID}/oauth2/v2.0/token&quot;);
  
  requestBearer.AddHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
  requestBearer.AddParameter(&quot;client_id&quot;, appRegistration.appId);
  requestBearer.AddParameter(&quot;client_secret&quot;, appRegistrationSecret);
  requestBearer.AddParameter(&quot;scope&quot;, &quot;499b84ac-1321-427f-aa17-267ca6975798/.default&quot;);
  requestBearer.AddParameter(&quot;username&quot;, username);
  requestBearer.AddParameter(&quot;password&quot;, password);
  requestBearer.AddParameter(&quot;grant_type&quot;, &quot;password&quot;);
              
  var responseBearer = clientBearer.ExecutePostAsync(requestBearer).Result;
  var azureBearerToken = JsonConvert.DeserializeObject&lt;AzureBearerToken&gt;(responseBearer.Content);
  var devopsBearerToken = azureBearerToken.token_type + &quot; &quot; + azureBearerToken.access_token;
  
  </code></pre>
  <p>Password Authentication was selected as the only viable non-interactive authentication method.  This user is replacing a ServicePrincipal/ManagedIdentity because Microsoft does not support those models for Devops REST API and not all Devops REST API functionality is available in the Azure CLI.</p>
  <p><a href="https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/authentication-guidance?view=azure-devops&amp;viewFallbackFrom=azure-devops%2F%2F" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/authentication-guidance?view=azure-devops&amp;viewFallbackFrom=azure-devops%2F%2F</a></p>
  <p>I now have a valid bearer Token</p>
  <pre><code>var devopsClient = new RestClient(@&quot;https://vssps.dev.azure.com&quot;);
  var patRequest = new RestRequest($&quot;{devopsOrganization}/_apis/tokens/pats?api-version=7.1-preview.1&quot;);
  patRequest.AddHeader(&quot;Authorization&quot;, devopsBearerToken);
  patRequest.AddHeader(&quot;Content-Type&quot;, &quot;application/json&quot;);
  var patListResponse = devopsClient.ExecuteGetAsync(patRequest).Result;
  </code></pre>
  <p>This response looks great and I know that the bearer token is working.  As this is a newly created AD user created through the Azure CLI I am expecting an empty array of PAT which I do receive in the response.</p>
  <p>So far, So good</p>
  <pre><code>//create a token
  var body = new
  {
      displayName = &quot;targetName&quot;,
      scope = &quot;app_token&quot;,
      validTo = validTo,
      allOrgs = true
  };
  patRequest.AddJsonBody(body);
  var patCreateResponse = devopsClient.ExecutePostAsync(patRequest).Result;
  </code></pre>
  <p>This final response is the problem and it contains the</p>
  <p>patTokenError  :  userIdRequired</p>