0

Ok, geesh, I am really having a hard time getting a full understanding of this Oauth stuff. I am making brutally slow process.

Now, I have the access_token and the refresh_token and the id_token:

{
    "access_token":"SlAV32hkKG"
    "id_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...",
    "token_type":"Bearer",
    "refresh_token":"76c5cdeecfd1543cfd4c3e7c054780ac",
    "expires_in": 3600
}

Next, I just need to use the above to make the final, actual API call. PLEASE believe me when I say that I have looked, but I am NOT finding a nice, simple, clean code example of how to make this call in a C# .NET app.

The API I am trying to consume does have their own documentation, and this is all they said about this step:

4. Make API Requests

Now that your application has a valid access token it can now use that token to make authenticated requests to PDK APIs. This is done by simply including the access token in the Bearer type Authorization header of your requests.The Authorization header should look similar to:

Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...GmUdC2H13YMCwc30Z3l5RCrbwQ

Which is less than helpful from a code writing standpoint. Are they saying that when I make the final (actual) API call that I do so with no actual authentication, but with the access_token included as a normal header? And if so, what is the name (key) of the header? And is Authorization: Bearer it's own header too?

This thread HAS been very helpful, but I'm still having trouble translating his examples into what I'm trying to do.

And because this will be an off-line app, I also need to somehow include the `resresh_token'.

I know that the protocol here on SO is to provide code that we've already tried and ask for suggestions on how to fix it. And normally that's what I do. But honestly, I'm lost here.

Thank you in advance!

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193

1 Answers1

1

You'll need to set the authorization header using the access_token. Here is an example:

var myUri = new Uri(fullpath);
var request = WebRequest.Create(myUri);
var httpRequest = (HttpWebRequest)request;
httpRequest.PreAuthenticate = true;
httpRequest.Headers.Add("Authorization", "Bearer " + access_token);
httpRequest.Accept = "application/json";

var webResponse = request.GetResponse();
var responseStream = webResponse.GetResponseStream();
if (responseStream == null) return null;
var reader = new StreamReader(responseStream, Encoding.Default);
var result = reader.ReadToEnd();
responseStream.Close();
webResponse.Close();
ザヘド
  • 614
  • 1
  • 6
  • 17