How can I access an API which requires OAuth 2.0 authorization, such as Google APIs
24 views (last 30 days)
Show older comments
Some REST APIs, such as Google Analytics' Core Reporting API, require authorization tokens, which must be obtained before the required data can be downloaded. Can anyone share or point to code on how to do this in MATLAB?
In particular I'm looking to use an App in the MATLAB-based ThingSpeak service to automatically download the number of unique visitors to my website from Google Analytics (behind an authentication firewall) daily, and save that information to a ThingSpeak channel.
1 Comment
Answers (2)
YH
on 28 Jan 2018
Edited: YH
on 28 Jan 2018
Go to the below link in any browser to retrieve your authorization code, using your client id:
https://accounts.google.com/o/oauth2/auth?client_id=YOUR_CLIENT_ID_FROM_GOOGLE_DEVELOPER_CONSOLE&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/analytics.readonly&response_type=code
Then, try the below, substituting your client id, client secret, and authorization code from before.
client_id = 'YOUR CLIENT ID FROM GOOGLE DEVELOPER CONSOLE';
client_secret = 'YOUR CLIENT SECRET FROM GOOGLE DEVELOPER CONSOLE';
url = 'https://accounts.google.com/o/oauth2/token';
redirect_uri = 'urn:ietf:wg:oauth:2.0:oob';
code = 'YOUR AUTHORIZATION CODE';
data = [...
'redirect_uri=', redirect_uri,...
'&client_id=', client_id,...
'&client_secret=', client_secret,...
'&grant_type=', 'authorization_code',...
'&code=', code];
response = webwrite(url,data);
access_token = response.access_token;
% save access token for future calls
headerFields = {'Authorization', ['Bearer ', access_token]};
options = weboptions('HeaderFields', headerFields, 'ContentType','json');
References:
2 Comments
Scott Snowden
on 19 Jul 2018
Thank you! This solved my problem, although I can't find a solution for 'send()'. Just to note - at the end of this, you'll need to call
webread('https://yoururlhere.com',options)
kees
on 8 Feb 2019
Edited: kees
on 8 Feb 2019
Dear YH,
I try to do the same thing for the freesound website, which expects this format:
"Authorization: Bearer {{access_token}}" 'httpss://freesound.org/apiv2/sounds/14854/download/'
I have tried to rebuild your example, but haven't succeeded, could you please point me in the right direction?
many thanks!
Kees
Hans Scharler
on 12 Mar 2016
To download data from ThingSpeak, you only need to send a request to a channel (public channel) or to channel with a read API key (private channel).
Here's how to read the last data from my private ThingSpeak channel that contains the last 10 power measurements for my house.
[data, time] = thingSpeakRead(readChannelID, 'Field', fieldID1, 'NumPoints', 10, 'ReadKey', readAPIKey);
To get access to thingSpeakRead, you need to install the ThingSpeak Support Toolbox.
You can also use "webread".
[data] = webread('https://api.thingspeak.com/channels/97871/fields/1.json?results=10&api_key=7MOXB8G515OAD94Q');
data.feeds.field1
Communities
More Answers in the ThingSpeak Community
See Also
Categories
Find more on Web Services in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!