Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Im trying to send a json string as part of content on the 2nd stage section of oauth2 to our client who is using salesforce along with the access_token.

They have verified, as far as they can tell, that everything is working fine via POSTMAN, they have given me the appropriate headers required, secretid,username, password etc.. We cannot find anything missing as far as headers.

I have debugged the code and validated that I am getting the correct access_token back from SF so all is fine in the first stage, but during the 2nd stage, I just constantly get a 401 error with session ID not valid/expired. I think I am really close on this, but for the life of me, am unable to get it working. Ive been at it for 6 days now and am banging head on the wall with it at the moment but I am hoping someone may have a solution or can point me in the right direction/spot if anything is wrong.

So I am using VS2017, vb.net 4.8
ClientId
ClientSecret
Username
Password
Token

are all prepopulated with valid values before beginning.

Using client As HttpClient = New HttpClient()
              Dim request = New FormUrlEncodedContent(New Dictionary(Of String, String) From {
                  {"grant_type", "password"},
                  {"client_id", ClientId},
                  {"client_secret", ClientSecret},
                  {"username", Username},
                  {"password", Password + Token}
              })
              request.Headers.Add("X-PrettyPrint", "1")

              Dim response = client.PostAsync(LOGIN_ENDPOINT, request).Result
              jsonResponse = response.Content.ReadAsStringAsync().Result



At this point, on the postasync, there is approx a half second delay, to me, indicating a connection between the code and the web url of the authorisation web page.

Debug printing the response and jsonresponse variables, I am getting a StatusCode: OK {200}

before proceeding to pull out some values by deserializing the json string with newtonsoft's json library like so:



Dim values = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(jsonResponse)
                AuthToken = values("access_token")
                InstanceUrl = values("instance_url")
                TokenType = values("token_type")



AuthToken is populated correctly, instanceurl, although not needed as I have this information given beforehand, is the same, tokentype is "Bearer".


SendJsonStrURL which is the 2nd stage URL that I need to send the AuthToken (Access_Token) as well as BigString, which is a pre-json serialised string into the conent - the problem here, is when the postasync happens - there is no delay whatsoever (whether that is relevant or not, unsure, just would have thought, if it were connecting and sending data, as small as the string is, there would have been something noticeable - also, the client cannot tell via logs as there are none, whether this 2nd stage is being connected to, only my initial first web page connection which is valid), it does not trip up any exceptions, just returns a 401 error and Session ID Invalid.


Dim content = New StringContent(BigString, Encoding.UTF8, "application/json")

                Dim method = New HttpMethod("PATCH")
                Dim nreq = New HttpRequestMessage(method, SendJsonStrURL) With {
                 .Content = content
                                                                         }
                nreq.Headers.Add("Authorization", "Bearer " + AuthToken)
                Dim postResponse = client.PostAsync(SendJsonStrURL, nreq.Content).Result
                jsonResponse = postResponse.Content.ReadAsStringAsync().Result
                ResStr = CType(postResponse.StatusCode, String)


At this point, debugging the jsonResponse and postResponse variables is returning Unauthorised and 401 error.

End Using


What I have tried:

Taking this out
nreq.Headers.Authorization = New AuthenticationHeaderValue("Bearer", AuthToken)

and replacing with
nreq.Headers.Add("Authorization", "Bearer " + AuthToken)



I'd pull my hair out but Im completely bald, turning into into something very frustrating that I am unable to do this 2nd stage Im thinking it is going to be something simple that I just have not been able to find, have looked everywhere (and will continue to as I have to solve this) but any time saving would be a major relief on my blood pressure! :) Please, if anyone can help you would be making my week - if only to save my sanity!
Posted
Updated 17-Dec-21 4:34am

1 solution

Figured it out.

Dim content = New StringContent(BigString, Encoding.UTF8, "application/json")

              Dim method = New HttpMethod("PATCH")
              Dim request2 = New HttpRequestMessage(method, SendJsonStrURL)
              request2.Headers.Add("Authorization", "Bearer " & AuthToken)
              request2.Headers.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
              request2.Headers.Add("X-PrettyPrint", "1")
              request2.Content = content
              Dim postResponse = client.SendAsync(request2).Result
              jsonResponse = postResponse.Content.ReadAsStringAsync().Result


I dont quite understand it, but it looks like it should have had a sendasync instead of post... not sure why - but it works. What a WEEK of headaches and frustration.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900