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

69 ビュー (過去 30 日間)
Oliver Woodford
Oliver Woodford 2016 年 1 月 19 日
編集済み: kees 2019 年 2 月 8 日
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 Center および File ExchangeWeb Services についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by