title: oAuth 2.0 & Bearer Token for TD Ameritrade
body: <p>I've been stuck on this issue with TD Ameritrade since early last week and figure its time to see if someone else has any idea for it.  Any concepts or suggestion would be apricated.</p>
  <p>When trying to access the TD Ameritrade API I can refresh a token without issue.</p>
  <p><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>
  <pre><code>private async Task&lt;string&gt; TokenAsync()
          {
              string Bearer = &quot;xxx&quot;;
              string Url = _API_URL + &quot;/v1/oauth2/token&quot;;
  
              using (var client = new HttpClient())
              {
                  client.BaseAddress = new Uri(_API_URL);
  
                  // We want the response to be JSON.
                  client.DefaultRequestHeaders.Accept.Clear();
                  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
  
                  // Build up the data to POST.
                  List&lt;KeyValuePair&lt;string, string&gt;&gt; postData = new List&lt;KeyValuePair&lt;string, string&gt;&gt;();
                  postData.Add(new KeyValuePair&lt;string, string&gt;(&quot;grant_type&quot;, &quot;authorization_code&quot;));
                  postData.Add(new KeyValuePair&lt;string, string&gt;(&quot;refresh_token&quot;, Bearer));
                  postData.Add(new KeyValuePair&lt;string, string&gt;(&quot;access_type&quot;, &quot;offline&quot;));
                  postData.Add(new KeyValuePair&lt;string, string&gt;(&quot;code&quot;, Bearer));
                  postData.Add(new KeyValuePair&lt;string, string&gt;(&quot;client_id&quot;, _UserAccount.Broker.BrokerConsumerKey));
                  postData.Add(new KeyValuePair&lt;string, string&gt;(&quot;redirect_uri&quot;, &quot;http://127.0.0.1&quot;));
  
                  FormUrlEncodedContent content = new FormUrlEncodedContent(postData);
  
                  // Post to the Server and parse the response.
                  HttpResponseMessage response = await client.PostAsync(&quot;/v1/oauth2/token&quot;, content);
                  string jsonString = await response.Content.ReadAsStringAsync();
                  object responseData = JsonConvert.DeserializeObject(jsonString);
  
                  // return the Access Token.
                  return ((dynamic)responseData).access_token;
              }
          }
  </code></pre>
  <p>But when trying to call an API for anything else I keep getting an authentication error 401.  The one I'm trying to use right now is the account information</p>
  <p><a href="https://developer.tdameritrade.com/account-access/apis/get/accounts/%7BaccountId%7D-0" rel="nofollow noreferrer">https://developer.tdameritrade.com/account-access/apis/get/accounts/%7BaccountId%7D-0</a></p>
  <pre><code>private async Task&lt;Model.User.Broker&gt; BrokerAsync()
          {
              //Task&lt;string&gt; tokenAsync = TokenAsync();
              //tokenAsync.Wait();
  
              string Token = &quot;xxx&quot;;
  
              using (var client = new HttpClient())
              {
                  client.BaseAddress = new Uri(_API_URL);
  
                  client.DefaultRequestHeaders.Accept.Clear();
                  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
                  client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, Token);
                  //client.DefaultRequestHeaders.Add(&quot;Authorization&quot;, &quot;Bearer &quot; + Token);
  
                  List&lt;KeyValuePair&lt;string, string&gt;&gt; postData = new List&lt;KeyValuePair&lt;string, string&gt;&gt;();
                  //postData.Add(new KeyValuePair&lt;string, string&gt;(&quot;Authorization&quot;, Token));
  
                  FormUrlEncodedContent content = new FormUrlEncodedContent(postData);
  
                  HttpResponseMessage response = await client.PostAsync(&quot;/v1/accounts/&quot; + _UserAccount.Broker.BrokerAccountNum, content);
                  string jsonString = await response.Content.ReadAsStringAsync();
                  object responseData = JsonConvert.DeserializeObject(jsonString);
  
  /// To be Parse Out to custom object model
                  return ((dynamic)responseData);
              }
          }
  </code></pre>
  <p>Now when I use the TD ameritrade account on their site I can get it to work without issue.  But when trying it in code after decoding the Token I get the 401 error.  The only thing I can see different is that on their site it has the oAuth sign in.  Where the only thing I have available in code is the bearer token.  I'm not sure if I'm suppose to sign in to oAuth with code and then bearer token?  Because all the code sample and doc I have reviewed have on or the other but not both and with TD Ameritrade I see no oAuth signin for it using code.</p>