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
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
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
编辑: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
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
社区
更多回答在 ThingSpeak Community
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Web Services 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!