Solved

Using curl in c#?

Posted on 2014-04-28
4
1,972 Views
Last Modified: 2014-04-29
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

0
Comment
Question by:JRockFL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40028681
What is the value of XXXXX in line 4? Is it: "Bearer PERSONAL_ACCESS_TOKEN"?
0
 
LVL 8

Author Comment

by:JRockFL
ID: 40029151
No, it is the literal value of the token I
Was provided.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40029161
You need to include the word "Bearer" before your token when you add it to the header.

e.g.

request.Headers("Authorization") = "Bearer XXXXX";

Open in new window

0
 
LVL 8

Author Closing Comment

by:JRockFL
ID: 40029268
Perfect, that was it!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question