14 Replies - 626 Views - Last Post: 19 September 2017 - 09:53 AM

#1 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Error: the remote server returned an error 400 bad request

Posted 15 September 2017 - 11:52 AM

I Double checked and webclient.uploadvalues looks good to me.

Does any see what could be the issuse?

I am using the following link for this example: https://code.msdn.mi...n-Said-e1d8d388


Error: the remote server returned an error 400 bad request

...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        If (Not String.IsNullOrEmpty(Request.QueryString("code"))) Then
            Code = Request.QueryString("code")
            GetDataInstagramToken()
        End If
    End If
End Sub

Function GetDataInstagramToken()
    Try
        Dim parameters As New NameValueCollection
        parameters.Add("client_id", Client_ID)
        parameters.Add("client_secret", ClientSecret)
        parameters.Add("grant_type", "authorization_code")
        parameters.Add("redirect_uri", Redirect_URI)
        parameters.Add("code", Code)

        Dim client As WebClient = New WebClient()
        Dim result = client.UploadValues("https://api.instagram.com/oauth/access_token", "POST", parameters)
    Catch ex As Exception
        error.text = ex.message
    End Try
End Function


Is This A Good Question/Topic? 0
  • +

Replies To: Error: the remote server returned an error 400 bad request

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,847
  • Joined: 12-June 08

Re: Error: the remote server returned an error 400 bad request

Posted 15 September 2017 - 12:10 PM

Perhaps you are missing a chunk after line 21?
var response = System.Text.Encoding.Default.GetString(result);

        // deserializing nested JSON string to object  
        var jsResult = (JObject)JsonConvert.DeserializeObject(response);  
        string accessToken = (string)jsResult["access_token"];  
        int id = (int)jsResult["user"]["id"];  
  
        //This code register id and access token to get on client side  
        Page.ClientScript.RegisterStartupScript(this.GetType(), "GetToken", "<script>var instagramaccessid=\"" + @"" + id + "" + "\"; var instagramaccesstoken=\"" + @"" + accessToken + "" + "\";</script>");  


As seen here:
http://www.c-sharpco...-instagram-api/
Was This Post Helpful? 1
  • +
  • -

#3 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Re: Error: the remote server returned an error 400 bad request

Posted 15 September 2017 - 12:20 PM

yes but I did some debuging and I am pretty sure my code stops running on line 21.

for example if you look at below code. output of myLabel is (flag1-flag2-flag3-the remote server returned an error 400 bad request) then it goes to 'catch' statment


Function GetDataInstagramToken()
	myLabel.text += "flag1"
	
    Try
        Dim parameters As New NameValueCollection
        parameters.Add("client_id", Client_ID)
        parameters.Add("client_secret", ClientSecret)
        parameters.Add("grant_type", "authorization_code")
        parameters.Add("redirect_uri", Redirect_URI)
        parameters.Add("code", Code)
		myLabel.text += "-flag2"
		
        Dim client As WebClient = New WebClient()
		myLabel.text += "-flag3"
        Dim result = client.UploadValues("https://api.instagram.com/oauth/access_token", "POST", parameters)
        myLabel.text += "-flag4"
		Dim response = System.Text.Encoding.Default.GetString(result)

        'deserializing nested JSON string to object
        Dim jsResult As JObject = JsonConvert.DeserializeObject(response)
        Dim accessToken As String = jsResult("access_token")
        Dim id As Int16 = jsResult("user")("id")

        'This code register id and access token to get on client side
        Page.ClientScript.RegisterStartupScript(this.GetType(), "GetToken", "<script>var instagramaccessid=\"" + @"" + id + "" + " \ "; var instagramaccesstoken=\"" + @"" + accessToken + "" + " \ ";</script>")

    Catch ex As Exception
         myLabel.text += "-" &ex.message
    End Try
End Function

This post has been edited by ikhlas06: 15 September 2017 - 12:36 PM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,847
  • Joined: 12-June 08

Re: Error: the remote server returned an error 400 bad request

Posted 15 September 2017 - 12:34 PM

Ah.. any reason you are not using breakpoints or outputting the ex.message in the try/catch?
Was This Post Helpful? 1
  • +
  • -

#5 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Re: Error: the remote server returned an error 400 bad request

Posted 15 September 2017 - 12:38 PM

i just used flag statment bc i though it would be faster. not really sure how to use break points. I will look into it some other time. I did update my last response by adding ex.message and updating the myLabel output

This post has been edited by ikhlas06: 15 September 2017 - 12:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Error: the remote server returned an error 400 bad request

Posted 15 September 2017 - 01:03 PM

If Instagram is responding with a 400 and saying you have a bad request, you probably need to check their API documentation to see what's required and why they might respond with a 400.
Was This Post Helpful? 1
  • +
  • -

#7 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Re: Error: the remote server returned an error 400 bad request

Posted 15 September 2017 - 01:14 PM

View PostArtificialSoldier, on 15 September 2017 - 01:03 PM, said:

If Instagram is responding with a 400 and saying you have a bad request, you probably need to check their API documentation to see what's required and why they might respond with a 400.


Yes, I did look at instagram api doc at https://www.instagra...authentication/

according to "Step Three: Request the access_token", parameters match up.

and I am pretty sure client_id and other parameters are correct becuase if I enter this link in webbrowser:

https://api.instagra...ponse_type=code

this works for me and I am able to login with instagram "code" vaule in URL.

other than that I am not sure what might be the issue
Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Error: the remote server returned an error 400 bad request

Posted 15 September 2017 - 01:33 PM

Can you get any information about the request in .NET, like you can get the list of headers and the request body? If so, you can compare that with what your browser is sending.
Was This Post Helpful? 1
  • +
  • -

#9 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Re: Error: the remote server returned an error 400 bad request

Posted 15 September 2017 - 03:30 PM

The insta api's documentation says I need to request it like this :

curl \-F 'client_id=CLIENT-ID' \
-F 'client_secret=CLIENT-SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=YOUR-REDIRECT-URI' \
-F 'code=CODE' \https://api.instagram.com/oauth/access_token


also I am using Install-Package Newtonsoft.Json -Version 10.0.3


rather than adding variables in 'parameters'... I just added a string so you can get a better idea of what I am sending it.


        Dim parameters As New NameValueCollection
        parameters.Add("client_id", "ssdfsdfsdfsdfsdf")
        parameters.Add("client_secret", "sdsdfdsfsdfsdfsdfsdfsdf")
        parameters.Add("grant_type", "authorization_code")
        parameters.Add("redirect_uri", "http://localhost:8979/UI/InstaHome.aspx")
        parameters.Add("code", "5ce168cc4dee45388af9d297324dda3a")
	
        Dim client As WebClient = New WebClient()
        Dim result = client.UploadValues("https://api.instagram.com/oauth/access_token", "POST", parameters)
   



only other thing i can think of is maybe i am having issue the way i am adding parameters

This post has been edited by ikhlas06: 15 September 2017 - 03:36 PM

Was This Post Helpful? 0
  • +
  • -

#10 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Error: the remote server returned an error 400 bad request

Posted 15 September 2017 - 05:29 PM

Maybe you are, that's why it would be helpful if there were a way to look at the actual request that it's sending instead of only looking at the code. You need to look at what is actually being sent to their servers, look for problems with that, and then figure out why it's doing that.

Here's the list of properties, you should be able to use that to get some information about what it's sending:
https://msdn.microso....webclient.aspx

You can also install a traffic analyzer and try to find the request that way:
https://stackoverflo...-nets-webclient
Was This Post Helpful? 1
  • +
  • -

#11 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Re: Error: the remote server returned an error 400 bad request

Posted 17 September 2017 - 12:06 PM

alright so I downloaded a traffic anlyzer (fiddler)

I reran my application and check logs in (fiddler)

I am not sure this is the issue becuase this is a warning not a error

15:00:36:0208 HTTPSLint> Warning: ClientHello record was 389 bytes long. Some servers have problems with ClientHello's greater than 255 bytes. https://github.com/ssllabs/research/wiki/Long-Handshake-Intolerance



also in fiddler under 'raw' tab i got the following message:

GET http://localhost:8979/UI/InstaHome.aspx?code=3f5cd4d0rb8117f20828d2d69 HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Referer: http://localhost:8979/UI/InstaHome.aspx?code=fcf54b3fddc9caad5
Accept-Language: en-US,en;q=0.5
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063
Accept-Encoding: gzip, deflate
Host: localhost:8979
Connection: Keep-Alive
Pragma: no-cache



i think issue i am having is with clientweb

This post has been edited by ikhlas06: 17 September 2017 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#12 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Error: the remote server returned an error 400 bad request

Posted 18 September 2017 - 10:01 AM

That's a request from your browser to your server, you need to find the one from your server to Instagram.
Was This Post Helpful? 1
  • +
  • -

#13 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Re: Error: the remote server returned an error 400 bad request

Posted 18 September 2017 - 06:16 PM

View PostArtificialSoldier, on 18 September 2017 - 10:01 AM, said:

That's a request from your browser to your server, you need to find the one from your server to Instagram.


connect to insta is alot harder than i though. I am not sure if it suppose to be this hard or i am just not getting it. I been trying to do this for couple months now. let me try this one last time.

btw thanks for trying



alright so i got these 2 request info from fiddler- raw tab:

1st request:

CONNECT api.instagram.com:443 HTTP/1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063
Content-Length: 0
Host: api.instagram.com
Connection: Keep-Alive
Pragma: no-cache

A SSLv3-compatible ClientHello handshake was found. Fiddler extracted the parameters below.

Version: 3.3 (TLS/1.2)
Random: 59 C0 6E 46 55 73 47 2F 22 AA F3 13 56 0A EF 02 53 7C AE 5C 1D 55 2F 20 C7 C2 8F 49 8C 88 F3 D3
"Time": 6/12/2007 11:48:41 AM
SessionID: empty
Extensions: 
	server_name	api.instagram.com
	status_request	OCSP - Implicit Responder
	elliptic_curves	unknown [0x1D), secp256r1 [0x17], secp384r1 [0x18]
	ec_point_formats	uncompressed [0x0]
	signature_algs	sha512_rsa, sha512_ecdsa, sha256_rsa, sha384_rsa, sha1_rsa, sha256_ecdsa, sha384_ecdsa, sha1_ecdsa, sha1_dsa
	SessionTicket	empty
	ALPN		h2, http/1.1
	extended_master_secret	empty
	0x0018		00 0A 03 02 01 00
	renegotiation_info	00
Ciphers: 
	[C02C]	TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
	[C02B]	TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
	[C030]	TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
	[C02F]	TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
	[C024]	TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
	[C023]	TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
	[C028]	TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
	[C027]	TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
	[C00A]	TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
	[C009]	TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
	[C014]	TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA
	[C013]	TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA
	[009D]	TLS_RSA_WITH_AES_256_GCM_SHA384
	[009C]	TLS_RSA_WITH_AES_128_GCM_SHA256
	[003D]	TLS_RSA_WITH_AES_256_CBC_SHA256
	[003C]	TLS_RSA_WITH_AES_128_CBC_SHA256
	[0035]	TLS_RSA_AES_256_SHA
	[002F]	TLS_RSA_AES_128_SHA
	[000A]	SSL_RSA_WITH_3DES_EDE_SHA

Compression: 
	[00]	NO_COMPRESSION


2nd request:

CONNECT www.instagram.com:443 HTTP/1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063
Content-Length: 0
Host: www.instagram.com
Connection: Keep-Alive
Pragma: no-cache

A SSLv3-compatible ClientHello handshake was found. Fiddler extracted the parameters below.

Version: 3.3 (TLS/1.2)
Random: 59 C0 6E 46 09 7C 69 D3 6A 28 B4 3F 60 2E 58 10 52 BB 22 2B 01 2F 56 3B 75 AC C0 93 2D 1F 7F FD
"Time": 6/12/2007 11:48:41 AM
SessionID: empty
Extensions: 
	server_name	www.instagram.com
	status_request	OCSP - Implicit Responder
	elliptic_curves	unknown [0x1D), secp256r1 [0x17], secp384r1 [0x18]
	ec_point_formats	uncompressed [0x0]
	signature_algs	sha512_rsa, sha512_ecdsa, sha256_rsa, sha384_rsa, sha1_rsa, sha256_ecdsa, sha384_ecdsa, sha1_ecdsa, sha1_dsa
	SessionTicket	empty
	ALPN		h2, http/1.1
	extended_master_secret	empty
	0x0018		00 0A 03 02 01 00
	renegotiation_info	00
Ciphers: 
	[C02C]	TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
	[C02B]	TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
	[C030]	TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
	[C02F]	TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
	[C024]	TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
	[C023]	TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
	[C028]	TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
	[C027]	TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
	[C00A]	TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
	[C009]	TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
	[C014]	TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA
	[C013]	TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA
	[009D]	TLS_RSA_WITH_AES_256_GCM_SHA384
	[009C]	TLS_RSA_WITH_AES_128_GCM_SHA256
	[003D]	TLS_RSA_WITH_AES_256_CBC_SHA256
	[003C]	TLS_RSA_WITH_AES_128_CBC_SHA256
	[0035]	TLS_RSA_AES_256_SHA
	[002F]	TLS_RSA_AES_128_SHA
	[000A]	SSL_RSA_WITH_3DES_EDE_SHA

Compression: 
	[00]	NO_COMPRESSION


Was This Post Helpful? 0
  • +
  • -

#14 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Re: Error: the remote server returned an error 400 bad request

Posted 19 September 2017 - 06:27 AM

View Postplanetodoo, on 19 September 2017 - 12:16 AM, said:

~~~~ mod: spammer comment removed. ~~~~



no thanks

This post has been edited by modi123_1: 19 September 2017 - 06:41 AM
Reason for edit:: spammer comment removed.

Was This Post Helpful? 0
  • +
  • -

#15 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Error: the remote server returned an error 400 bad request

Posted 19 September 2017 - 09:53 AM

Do both of those requests result in a 400, or which one?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1