Heyy all,

 

i have a question. I try to connect to the ning api with webreferences in ASP.Net. But everytime i try to connect it doesn't work. I get a error.

 

Does someone knows how this works,

 

Greetz,

 

Olaf

 

 

Views: 859

Reply to This

Replies to This Discussion

good to know!

Hey Franco,

I also have some problems with Authorization. Maybe you could help me ?

I have managed to get oauth token and everything, but when I try get the user list with 

https://external.ningapis.com/xn/rest/nfs-online/1.0/User/alpha?xn_...

Then it says "The oauth_consumer_key is unknown to the Service Provider. No consumer key was provided", but the key is included. I have made some xxxx in the id's here ;-)

 

Authorization: OAuth oauth_version="1.0",oauth_signature_method="PLAINTEXT",oauth_consumer_key="29e3301f-xxxx-41fa-ac7e-04264d7c0a6a",oauth_token="750473e9-xxxx-498d-9963-8785d41177fc",oauth_timestamp="1315036786",oauth_token_secret="baabee44-xxx-4299-aae2-69e2beea075a",oauth_signature="abf826df-xxx-468d-bd25-1444bffb1713%26"

 

The key I'm including is the key I get from the token result. And also are identical with the original key.

I'm using cUrl and C#.

 

So maybe it's a cUrl option I need to set/remove ???

 

Thanks

Jan

 


Franco Mercalli said:

Yes, I confirm that the issue is with Poster: it seems that it messes things up a bit with the Authorization header.

I tried the same case with cURL and it worked perfectly.

Sorry for annoying you with this.


Franco Mercalli said:

In fact, I'm using the Poster add-on for Firefox (https://addons.mozilla.org/en-US/firefox/addon/poster/). May be the problem is with this tool...

Hi Jan,

Below is a cURL command (for Windows command prompt) that eventually worked for me (key/token/secret numbers are just random examples):

 

curl "https://external.ningapis.com/xn/rest/test-60e19eo4d305919359c2/1.0/Photo/recent?xn_pretty=true&fields=image.url,title&count=2" -H "Authorization: OAuth oauth_consumer_key=\"f966e7c6-a795-4774-9afb-2f68068d98b5\",oauth_token=\"33e6607b-ef42-457b-a23e-1a85163e5e87\",oauth_signature_method=\"PLAINTEXT\",oauth_signature=\"cbdefd3c-2017-46b0-9b54-ea965e8faa52%%267201bede-9aba-4453-8584-4a17ed2b5b5b\",oauth_timestamp=\"1310431842\",oauth_nonce=\"kllo9940pd9333jh\",oauth_version=\"1.0\"" -k

 

The difference I can see with yours is that I have no oauth_token_secret paramater, rather the comsumer key secret and the token secret are joined together in the oauth_signature parameter, with the '&' character separating them (in the code above this is '%%26' instead of '%26' because in Windows command prompt you need to escape '%' as well):

<consumer key secret>&<token secret>

Later, I discovered that some of the parameters I used in the cURL command above are not actually needed (e.g. oauth_nonce).

Please find below a PHP function that I eventually implemented to access Ning API, that seems to work fine. You can check out how the 'Authorization: Oauth' header is concocted in the $header variable:

 

function ningapi_get($resource, $obj_fmt = true) {
    $curl = curl_init("https://external.ningapis.com/xn/rest/" . NING_NETWORK . "/1.0/$resource" . ($obj_fmt ? '' : '&xn_pretty=true'));
    $header = 'Authorization: OAuth ';
    $header .= 'oauth_consumer_key="' . CONSUMER_KEY . '",';
    $header .= 'oauth_token="' . TOKEN . '",';
    $header .= 'oauth_signature="' . CONSUMER_SECRET . '%26' . TOKEN_SECRET . '",';
    $header .= 'oauth_signature_method="PLAINTEXT",oauth_version="1.0"';
    curl_setopt($curl, CURLOPT_HTTPHEADER, array($header));
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $r = curl_exec($curl);
    curl_close($curl);
    return $obj_fmt ? json_decode($r) : '<pre>' . ($r) . '</pre>';
}

 

I hope this can help a little

Franco

Hey Franco

I'm so glad to hear from you !

I have specified it a bit more, and changed it so it looks like your code

 

My post to https://external.ningapis.com/xn/rest/nfs-online/1.0/User/alpha?xn_...

 

My C# code

 

Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

Easy easy = new Easy();

Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);

easy.SetOpt(CURLoption.CURLOPT_URL, method);

 

oAuthSignature = consumerSecret + "%26" + oAuthTokenSecret;

string header = "Authorization: OAuth " +

                "oauth_consumer_key=\"" + consumerKey + "\"," +

                "oauth_token=\"" + oAuthToken + "\"," +

                "oauth_signature=\"" + oAuthSignature + "\"," +

                "oauth_signature_method=\"PLAINTEXT\",oauth_version=\"1.0\"";

 

easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, header);

easy.SetOpt(CURLoption.CURLOPT_POST, 0);

easy.SetOpt(CURLoption.CURLOPT_HEADER, 1);

easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, 1);

easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false);

 

easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

easy.Perform();

easy.Cleanup();

Curl.GlobalCleanup();

 

The header content (with some xxxx in)

Authorization: OAuth oauth_consumer_key="29e3301f-xxxx-41fa-ac7e-04264d7c0a6a",oauth_token="536a60ff-xxxx-479a-a0e5-594a6a684528",oauth_signature="abf826df-xxxx-468d-bd25-1444bffb1713%267cba6319-xxxx-4a28-84af-8da1aa63d6f9",oauth_signature_method="PLAINTEXT",oauth_version="1.0"

 

And the result !

And it seems it never gets the oauth_consumer_key ? But it's includede in my header.

I notised something, you are using some array method to save the header. Maybe I need to do something like that ?

And you are also using curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);, and I don't have that option in my C# version

 

 

HTTP/1.1 401 Unauthorized

Date: Sat, 03 Sep 2011 10:34:21 GMT

Server: Ning HTTP Server 2.0

Expires: Thu, 01 Jan 1970 00:00:00 GMT

Set-Cookie: xn_visitor=714636ca-c8f4-4d7b-80ac-62f39b1907d5;Path=/;Domain=.external.ningapis.com;Expires=Tue, 31-Aug-21 10:34:21 GMT

Set-Cookie: ning_session=ywiuhJuXw1sjVpxxIiUSLn13aGPiI72Nakf8qk0PVahbyxyClIxmu85nIHmCgHtCDBQpc6rnGP8=;Path=/;Domain=.external.ningapis.com;Expires=Sat, 03-Sep-11 11:34:21 GMT

X-XN-Trace-Token: 22d56116-07e0-4307-a2b5-46d10dde164d

Access-Control-Allow-Origin: *

Access-Control-Max-Age: 600

Access-Control-Allow-Credentials: true

Access-Control-Allow-Methods: GET, POST, PUT, DELETE, HEAD, OPTIONS

Access-Control-Allow-Headers: Authorization, X-Authorization

Content-Type: application/json

Cache-Control: no-cache="Set-Cookie"

Transfer-Encoding: chunked

 

{

  "success" : false,

  "reason" : "The oauth_consumer_key is unknown to the Service Provider. No consumer key was provided.",

  "status" : 401,

  "code" : 1,

  "subcode" : 7,

  "trace" : "22d56116-07e0-4307-a2b5-46d10dde164d"

}

 

 

 

Jan,

This is the meaning of the two options you mentioned (from PHP manual):

 

CURLOPT_HTTPHEADER: An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')


CURLOPT_RETURNTRANSFER: TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

 

Unfortunately, I do not know C#, thus I cannot say if they are meaningful in your case. May be you do.

When I got the same error message that you received (reason "The oauth_consumer_key is unknown to the Service Provider. No consumer key was provided", status 401, code 1 and subcode 7) it was because, although the header was properly constructed, it probably was not sent out correctly by the tool I was using (Poster for Firefox).

In fact, you can get the same message if, for instance, you send out the cURL command with simply no header specified at all.

As your header seems now OK, I'm wondering if you are dealing with a similar problem.

Perhaps you can try to send the request with cURL for Windows, just to check if with this tool the header works. If it does, at least you will now that the problem is not with the header but with the C# cURL library.

I send you in attachment the version of cURL for Windows that I used, with the batch file to send the Ning API command (you need to substitute 'xxxx' strings with your own values).

It should work nicely if you put everything in the same directory and run the batch file.

 

 

 

Attachments:

Hey Franco

Thank you SO much for your help !!

I got your curltest, and that works just fine with my credentials. So far so good !!!!

So now it's time to investigate why C# cUrl library don't work.

Is the header just a text block with carriage returns to separate the items ?

 

I'm unsure about the CURLOPT_RETURNTRANSFER  because, I don't use that when I use ny cUrl to get the oauth_token, and here I get a fine json result back

 

So maybe I also need the content-type you wrote about

 

Many many thanks Franco !!

 

:-)

 



Franco Mercalli said:

Jan,

This is the meaning of the two options you mentioned (from PHP manual):

 

CURLOPT_HTTPHEADER: An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')


CURLOPT_RETURNTRANSFER: TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

 

Unfortunately, I do not know C#, thus I cannot say if they are meaningful in your case. May be you do.

When I got the same error message that you received (reason "The oauth_consumer_key is unknown to the Service Provider. No consumer key was provided", status 401, code 1 and subcode 7) it was because, although the header was properly constructed, it probably was not sent out correctly by the tool I was using (Poster for Firefox).

In fact, you can get the same message if, for instance, you send out the cURL command with simply no header specified at all.

As your header seems now OK, I'm wondering if you are dealing with a similar problem.

Perhaps you can try to send the request with cURL for Windows, just to check if with this tool the header works. If it does, at least you will now that the problem is not with the header but with the C# cURL library.

I send you in attachment the version of cURL for Windows that I used, with the batch file to send the Ning API command (you need to substitute 'xxxx' strings with your own values).

It should work nicely if you put everything in the same directory and run the batch file.

 

 

 

That's good news Jan!

At least, now you know that your credentials are OK.

From here on I cannot be of much help, though, as I do not know C# and have limited experience with cURL.

The only thing that attracts my attention in your code is the matter with the CURLOPT_HTTPHEADER.

In the PHP library this has to be used with an array of headers (possibly, an array with just one element):

 

CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

 

From this other example, it seems that the same be true for other languages: http://curl.haxx.se/libcurl/c/postit2.html. May be you have to use an array in C# as well?

However, that's just a feeling and I'm not actually sure of what I'm saying...

Hopefully, a more knowledgeable colleague will join this discussion!

Franco

Hey Franco my new best friend ;-)

I got the authentication part to work with cUrl, but were unable to use it to the second call with data in the header.

So I have used a normal .NET WebClient request with the right header, and that works just perfectly.

So now I'm a happy dude !!

Thanks a lot for your help !!

 

Jan 


Franco Mercalli said:

That's good news Jan!

At least, now you know that your credentials are OK.

From here on I cannot be of much help, though, as I do not know C# and have limited experience with cURL.

The only thing that attracts my attention in your code is the matter with the CURLOPT_HTTPHEADER.

In the PHP library this has to be used with an array of headers (possibly, an array with just one element):

 

CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

 

From this other example, it seems that the same be true for other languages: http://curl.haxx.se/libcurl/c/postit2.html. May be you have to use an array in C# as well?

However, that's just a feeling and I'm not actually sure of what I'm saying...

Hopefully, a more knowledgeable colleague will join this discussion!

Franco

You are welcome, Jan!

Reply to Discussion

RSS

Blog Posts

Getting Started

Posted by Kyle Ford on October 13, 2010 at 8:00am 3 Comments

A Note on API Pricing

Posted by Phil McCluskey on October 1, 2010 at 8:55am 0 Comments

Welcome to Build!

Posted by Kyle Ford on September 30, 2010 at 8:30pm 1 Comment

Ning Status

All systems Go!

All Ning Networks are fully operational and speedy. Things are humming along nicely.

© 2017   Created by Build Team.   Powered by

Badges  |  Report an Issue  |  Terms of Service