Link to home
Start Free TrialLog in
Avatar of JRockFL
JRockFLFlag for United States of America

asked on

Using curl in c#?

I'm trying to use the square connect api with c#
I'm getting the following error.

"The remote server returned an error: (401) Unauthorized."

 
  Open your favorite command-line application (such as Terminal if you're using a Mac) and run the following curl command, providing your access token where indicated:

    curl -H "Authorization: Bearer PERSONAL_ACCESS_TOKEN" https://connect.squareup.com/v1/me/payments

    The -H option adds a header to your HTTP request with the format specified in quotation marks. This header is required for all requests you make to the Connect API.

    When the curl command returns, it prints JSON output representing your payment history (up to the 2000 most recent payments). If you haven't taken a Square payment yet, the output is simply an empty array, [].

Nice work, you've successfully downloaded your payment history with the Connect API.

Here is my code, any ideas?

WebRequest request = WebRequest.Create("https://connect.squareup.com/v1/me/payments");
request.ContentType = "application/json";
request.Method = "GET";
request.Headers("Authorization") = "XXXXX";

HttpWebResponse response = null;
string responseMessage = null;
response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK) {
    using (Stream stream = response.GetResponseStream()) {
    using (StreamReader reader = new StreamReader(stream)) {
        responseMessage = reader.ReadToEnd();
        }
    }
}

Assert.IsNotNull(responseMessage);

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

What is the value of XXXXX in line 4? Is it: "Bearer PERSONAL_ACCESS_TOKEN"?
Avatar of JRockFL

ASKER

No, it is the literal value of the token I
Was provided.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JRockFL

ASKER

Perfect, that was it!