locked
OAUTH2 C# POST TD Ameritrade Getting error RRS feed

  • Question

  • User-2004102533 posted

     I am now attempting to use the refresh_token that is valid for 90 days to get an auth token.  I get an error "":\"Failed to resolve API Key variable request.header.un\"\n \t\t}\n" this is from my C# . I am sure my refresh_token is accurate.  Here is the code:

               string     url = string.Format("https://api.tdameritrade.com/v1/oauth2/token");
                RestClient client = new RestClient(url);
                RestRequest postRequest = new RestRequest(Method.POST);
                postRequest.AddHeader("cache-control", "no-cache");
                postRequest.AddHeader("content-type", "application/x-www-form-urlencoded");
                postRequest.AddParameter("application/x-www-form-urlencoded", "grant_type=refresh_token&    refresh_token=cNxNLQlM@AMER.OAUTHAP", ParameterType.RequestBody);//, ParameterType.RequestBody)

                

       //  This is where the post is executed
                IRestResponse response = client.Execute(postRequest);

    Monday, September 2, 2019 7:22 PM

All replies

  • User283571144 posted

    Hi DoctorWhoKnew,

    According to the tdameritrade auth article:

    To request a new access token, make a Post Access Token request with your refresh token using the following parameter values:

    grant_type: refresh_token
    refresh_token: {REFRESH TOKEN}
    client_id: {Consumer Key}

    I found your requrest parameter doesn't contain the client id, I guess this is why your codes doesn't work, I suggest you could modify your postRequest.AddParameter method and add the client id and try again.

    Best Regards,

    Brando

    Tuesday, September 3, 2019 1:53 AM
  • User55524441 posted

    Hi,

    Were you able to get it fixed? Can you please share the fix?

    Thanks

    Wednesday, September 25, 2019 9:38 AM
  • User1465842319 posted

    Hello, 

    Any Luck on it. ?

    If Yes, Kindly share the solution

    Thursday, January 23, 2020 7:12 PM
  • User-2004102533 posted

    I did find a solution. I needed to encode the refresh_token.  Here is the code:

    var parameters = new Dictionary<string, string>
    { { "grant_type", "refresh_token" }, { "refresh_token", refresh_token_ }, { "client_id", client_id} };
    var encodedContent = new FormUrlEncodedContent(parameters);

    var response = await _httpclient.PostAsync(AmerOAuthUrl, encodedContent).ConfigureAwait(false);
    if (response.StatusCode == HttpStatusCode.OK)
    {
    // Do something with response. Example get content:
    var responseContent = await response.Content.ReadAsStringAsync ().ConfigureAwait (false);
    AmerAccessToken amerAccessToken = JsonConvert.DeserializeObject<AmerAccessToken>(responseContent);
    accessToken = amerAccessToken.access_token;
    accessTokenTime = amerAccessToken.expires_in;
    }

    Friday, January 24, 2020 12:37 PM
  • User1787365979 posted

    Does anyone have any sample code to get me started, some of this is a bit over my head.  If I could just get a start I think I could hack my way through it.  Thanks

    Friday, January 15, 2021 3:03 PM