using data from an api call (Using Oauth1)

Apr 26, 2015 at 2:16 AM
Hi I have called the twitter API Which returns the profile picture address along with loads of data that I don't need.
How do I put just the profile picture into a variable so I can do a separate call and download that file. I am using the Oauth1 sample.

Any help would be appreciated.

Mark
Coordinator
Apr 27, 2015 at 6:45 PM
Hi Mark,

I'm super familiar with the Twitter API. It sounds like the HTTP response returns a bunch of data. What is the format? If it is a JSON object then you probably just need to construct a web::json::value with the web::http::http_response::extract_json API. Then you can retrieve the image address information to make a new request.

Steve
Apr 27, 2015 at 7:54 PM
Hi Steve,

Thank you for the help.
The problem I am having is im not quite sure how to construct it. Yeah it returns a json object.
Is there any chance you could give me an example?

Kind regards.

Mark


Sent from Samsung Mobile


Coordinator
Apr 27, 2015 at 7:56 PM

Hi Mark,

I’m confused, you are not sure how to construct what exactly? The json::value object? Or another http_request object for the second request?

Steve

Apr 27, 2015 at 8:07 PM
Hi Steve,

The value object would I need to iterate over the json object and extract just the parts that I need?

Mark

Sent from Samsung Mobile


Apr 27, 2015 at 8:09 PM
Hi Steve,

The value object would I need to iterate over the json object and extract just the parts that I need?

Mark


Sent from Samsung Mobile


Coordinator
Apr 27, 2015 at 8:16 PM
Hi Mark,

Yes exactly, the http_response::extract_json() method returns a task of a json::value. From the json::value object you can index into any fields the JSON object has. You can use the web::json::value::at(...) method or the operator[] to access individual JSON object fields.

Take a look at the json::value reference documentation I referenced earlier in this discussion thread. You also can take a look at some of the JSON test cases in the repository. It should be pretty straightforward once you know what fields you are looking for, take a look at the Twitter API documentation for that.

Steve
Apr 27, 2015 at 8:30 PM
Edited Apr 27, 2015 at 8:30 PM
Hi Steve,

So say I used ->

ucout << web::json::value::at("follower_count") << endl;


would that give me only the followers count? is that correct?

I really appreciate your help.

Mark
Coordinator
Apr 27, 2015 at 8:34 PM

First of all you need an instance of the web::json::value class. If that is the name of the field you want in the JSON object, then yes that could work. I can’t figure this part out for you. It will return another JSON object to you, which you will need to examine or know what format the data is stored in, for example a number or a string.

Steve

Apr 27, 2015 at 9:02 PM
I ma going by the oauth 1 example so I think I might need to change it. I have this so far

http_client api(U("https://api.twitter.com/1.1/"), m_http_config);
ucout << api.request(methods::GET, U("/users/show.json?screen_name=moonie878")).get().extract_json().get();
json::value obj = json::value::object();
obj = json::value::object("followers_count");
ucout << obj;

I think I have it wrong though. as it does not print out the followers count I need to make it as a string i think.
Coordinator
Apr 27, 2015 at 9:27 PM

Why are you constructing new JSON objects in your code? You aren’t even referring to the JSON object from the extract_json(…) API call. I think you have some confusion somewhere. You need the object instance to call instance methods.

Do something more like the following:

json::value jsonObj = api.request(…).get().extract_json().get();

const json::value &followers = jsonObj(U(“followers_count”));

// Depending on what JSON type the ‘followers_count’ is you could get the count using one of the ‘as_*’ methods. For example to get it as an integer:

// int count = followers.as_integer();

Steve

Apr 27, 2015 at 11:19 PM
Hi Steve,

the only problem with this is ->

const json::value &followers = jsonObj(U(“followers_count”));

I get an error saying -> 1 IntelliSense: call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type \Oauth1Client\Oauth1Client.cpp 286 36 Oauth1Client120

Mark
Apr 28, 2015 at 12:02 AM
YESSSSS Thank you so much.

I just needed the .at.

I really do appreciate your help.

Mark