Solved

Using curl in c#?

Posted on 2014-04-28
4
1,788 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
  • 2
  • 2
4 Comments
 
LVL 74

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 74

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now