MATLAB Answers

0

How can I access an API which requires OAuth 2.0 authorization, such as Google APIs

Asked by Oliver Woodford on 19 Jan 2016
Latest activity Edited by kees
on 8 Feb 2019
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

Yuebin Zhou 2016 年 1 月 22 日
Can you further explain what you want to do with MATLAB?

Sign in to comment.

2 Answers

回答者: YH
2018 年 1 月 28 日
編集済み: YH
2018 年 1 月 28 日

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 2018 年 7 月 19 日
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
2019 年 2 月 8 日
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

Sign in to comment.


Answer by 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

  1 Comment

Oliver Woodford 2016 年 3 月 12 日
Thanks, Hans. However, I'm not looking to download data from a ThingSpeak channel. I want to download data from Google Analytics, data which is hidden behind an authentication process. I've edited my question to make that clearer.

Sign in to comment.