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

조회 수: 26 (최근 30일)
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.

답변 (2개)

YH
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
Scott Snowden
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
kees 2019년 2월 8일
편집: 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

댓글을 달려면 로그인하십시오.


Hans Scharler
Hans Scharler 2016년 3월 12일
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
Oliver Woodford
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.

댓글을 달려면 로그인하십시오.

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by