Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using curl in c#?

Posted on 2014-04-28
4
Medium Priority
?
2,041 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

670 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