Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Server code with twitter api

Hello Support
I am using server code access Twitter's api. I have succesfully connected received the required data. On using the command

response.success(XHRResponse.body, "application/json");

I received

"token_type": "bearer",
"access_token": "*********"
}

I now wish to transfer the "token_type" and "access_token" data in a variable . I tried

var token_type = XHRResponse.body[0] ["token_type"];

but this has failed. What is the correct code?

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Server code with twitter api

Hello Mark,

Please use JS code below for that:
prevar token_type = XHRResponse.body.token_type;/pre

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Server code with twitter api

Thanks that has worked. I also have received the following data from another twitter api. The response body results are

[ { name: 'Worldwide', placeType: { code: 19, name: 'Supername' }, url: 'http://where.yahooapis.com/v1/place/1', parentid: 0, country: '', woeid: 1, countryCode: null }, { name: 'Winnipeg', placeType: { code: 7, name: 'Town' }, url: 'http://where.yahooapis.com/v1/place/2972', parentid: 23424775, country: 'Canada', woeid: 2972, countryCode: 'CA' }, { name: 'Ottawa', placeType: { code: 7, name: 'Town' }, url: 'http://where.yahooapis.com/v1/place/3369', parentid: 23424775, country: 'Canada', woeid: 3369, countryCode: 'CA' }, { name: 'Quebec', placeType: { code: 7, name: 'Town' }, url: 'http://where.yahooapis.com/v1/place/3444', parentid: 23424775, country: 'Canada', woeid: 3444, countryCode: 'CA' }, { name: 'Montreal', placeType: { code: 7, name: 'Town' }, url: 'http://where.yahooapis.com/v1/place/3534', parentid: 23424775, country: 'Canada', woeid: 3534, countryCode: 'CA' } ]

I now wish to transfer the "name" and "country" data in a variable . I tried

data = JSON.parse(XHRResponse.body);
var city = XHRResponse.body.name;
var country = XHRResponse.body.country;
console.log(city);

The console.log(city) results are undefined.

How do I fix this?

Also

{ '0': { TwitterCity: 'Worldwide', TwitterCountry: '' }, '1': { TwitterCity: 'Winnipeg', TwitterCountry: 'Canada' }, '2': { TwitterCity: 'Ottawa', TwitterCountry: 'Canada' }, '466': { TwitterCity: 'Okayama', TwitterCountry: 'Japan' } }

I now wish to transfer the "TwitterCity" and "TwitterCountry" data in a variable . I tried

data = JSON.parse(XHRResponse.body);
var TwitterCity = XHRResponse.body.name;
var TwitterCountry = XHRResponse.body.country;
console.log(TwitterCity);

The console.log(TwitterCity) results are undefined.

How do I fix this?

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Server code with twitter api

Hello Support
Please ignore what I requested above as I have made some progress. However I also have received the following data from another twitter api. The response body results are

[{"trends":[{"name":"#FelizMartes","url":"http:\/\/twitter.com\/search?q=%23FelizMartes","promoted_content":null,"query":"%23FelizMartes","tweet_volume":18177},{"name":"#DiadelaMusica","url":"http:\/\/twitter.com\/search?q=%23DiadelaMusica","promoted_content":null,"query":"%23DiadelaMusica","tweet_volume":11065},{"name":"#Geri\u00C7ekmekYetmez\u0130ptalEdilsin","url":"http:\/\/twitter.com\/search?q=%23Geri%C3%87ekmekYetmez%C4%B0ptalEdilsin","promoted_content":null,"query":"%23Geri%C3%87ekmekYetmez%C4%B0ptalEdilsin","tweet_volume":12423},{"name":"#\u0627\u0647\u0645\u0645\u064A\u0632\u0647\u0628\u0627\u0644\u0627\u0646\u062B\u064A","url":"http:\/\/twitter.com\/search?q=%23%D8%A7%D9%87%D9%85%D9%85%D9%8A%D8%B2%D9%87%D8%A8%D8%A7%D9%84%D8%A7%D9%86%D8%AB%D9%8A","promoted_content":null,"query":"%23%D8%A7%D9%87%D9%85%D9%85%D9%8A%D8%B2%D9%87%D8%A8%D8%A7%D9%84%D8%A7%D9%86%D8%AB%D9%8A","tweet_volume":91445},{"name":"#\u3044\u3044\u30C4\u30A4\u30F3\u30C6\u30FC\u30EB\u306E\u65E5","url":"http:\/\/twitter.com\/search?q=%23%E3%81%84%E3%81%84%E3%83%84%E3%82%A4%E3%83%B3%E3%83%86%E3%83%BC%E3%83%AB%E3%81%AE%E6%97%A5","promoted_content":null,"query":"%23%E3%81%84%E3%81%84%E3%83%84%E3%82%A4%E3%83%B3%E3%83%86%E3%83%BC%E3%83%AB%E3%81%AE%E6%97%A5","tweet_volume":27441},{"name":"\u592B\u5A66\u306E\u65E5","url":"http:\/\/twitter.com\/search?q=%E5%A4%AB%E5%A9%A6%E3%81%AE%E6%97%A5","promoted_content":null,"query":"%E5%A4%AB%E5%A9%A6%E3%81%AE%E6%97%A5","tweet_volume":183048},{"name":"Delhi Suffers Airtel","url":"http:\/\/twitter.com\/search?q=%22Delhi+Suffers+Airtel%22","promoted_content":null,"query":"%22Delhi+Suffers+Airtel%22","tweet_volume":null},{"name":"Salih M\u00FCslim","url":"http:\/\/twitter.com\/search?q=%22Salih+M%C3%BCslim%22","promoted_content":null,"query":"%22Salih+M%C3%BCslim%22","tweet_volume":null},{"name":"#ReplaceSongTitleWithMakar","url":"http:\/\/twitter.com\/search?q=%23ReplaceSongTitleWithMakar","promoted_content":null,"query":"%23ReplaceSongTitleWithMakar","tweet_volume":null},{"name":"#\u0642\u0647\u0648\u0647\u0627\u0644\u0635\u0628\u0627\u062D","url":"http:\/\/twitter.com\/search?q=%23%D9%82%D9%87%D9%88%D9%87%D8%A7%D9%84%D8%B5%D8%A8%D8%A7%D8%AD","promoted_content":null,"query":"%23%D9%82%D9%87%D9%88%D9%87_%D8%A7%D9%84%D8%B5%D8%A8%D8%A7%D8%AD","tweet_volume":null},{"name":"#EstamosDeFestival","url":"http:\/\/twitter.com\/search?q=%23EstamosDeFestival","promoted_content":null,"query":"%23EstamosDeFestival","tweet_volume":null}],"as_of":"2016-11-22T12:02:20Z","created_at":"2016-11-22T11:57:19Z","locations":[{"name":"Worldwide","woeid":1}]}]

I now wish to transfer the "name" and "tweet_volume" data in a variable . I tried

data = JSON.parse(XHRResponse.body);
var name = XHRResponse.body.name;
var tweet_volume = XHRResponse.body.tweet_volume;
console.log(name);

The console.log(name) results are undefined.

How do I fix this?

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Server code with twitter api

Not worry. I figured it out

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Server code with twitter api

Glad to hear things are sorted out now!

Return to “Issues”