Solved

Using curl in c#?

Posted on 2014-04-28
4
1,904 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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