title: TDAmeritrade API Authentication Error: Failed to resolve API Key variable request.header.un
body: <p>I'm having difficulty obtaining an access token from the token endpoint using PHP (<a href="https://developer.tdameritrade.com/authentication/apis/post/token-0" rel="nofollow noreferrer">https://developer.tdameritrade.com/authentication/apis/post/token-0</a>)</p>
  
  <p>Particularly, I'm getting the following error: </p>
  
  <blockquote>
    <p>{ "error":"Failed to resolve API Key variable request.header.un" }</p>
  </blockquote>
  
  <p>My request, using PHP, is:</p>
  
  <pre><code>  $url = 'https://api.tdameritrade.com/v1/oauth2/token';
  
    $client_id = $customer_key ;
  
     $redirect_uri = $redirect_URL;
    $myvars = array("grant_type" =&gt; "authorization_code"
                    , "access_type" =&gt; "offline"
                    , "client_id" =&gt; $client_id
                     , "redirect_uri" =&gt; $redirect_URL
                    , "code" =&gt; "$code");
  
    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt( $ch, CURLOPT_VERBOSE, 1);
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/x-www-form-urlencoded; charset=utf-8'
  
            ));
  
    $response = curl_exec( $ch );
    echo "&lt;br&gt;token = '$response'\n";
  
  }
  
  </code></pre>
  
  <p>Thanks!</p>