Access restricted google docs via matlab

조회 수: 14 (최근 30일)
Doug Weir
Doug Weir 2022년 5월 18일
댓글: Doug Weir 2022년 5월 18일
I'm looking for some help on oauth2 authorisation using websave (or alternative) to access google docs via matlab.
If a google doc file does not have any acess restrictions, interaction is straight-forward, e.g. to save locally as a txt file:
docid = '16QTaks53H7FMBuT-zNlrj-V06aDX-K_hDCIlxBnLcos'; % example id, not a real google doc
websave([pwd '\output.txt'],['https://docs.google.com/document/d/',docid,'/export?format=txt']); %
Unfortunately if the file is access restricted, things seem to become more complex. The simple username/password approach of weboptions does not work.
I had hoped that I would be able to work it out based on the RunOnce function in the matlab2sheets FeX submission. Although this funciton runs smoothly, I have not worked out how to use the access tokens it generates to read restricted google docs (or sheets for that matter), but figured it would look something like:
load('google_tokens.mat') % google_tokens.mat is output from RunOnce and contains aSheets
headerFields = {'Authorization', ['Bearer: ', aSheets]};
options = weboptions('HeaderFields', headerFields);
docid = '16QTaks53H7FMBuT-zNlrj-V06aDX-K_hDCIlxBnLcos';
websave([pwd '\output.txt'],['https://docs.google.com/document/d/',docid,'/export?format=txt'],options); %
Does anyone know if acessing google docs in this way is possible, or any different solutions? I'm out of my comfort zone here, so any help is very much appreciated, thanks!
  댓글 수: 1
Doug Weir
Doug Weir 2022년 5월 18일
I've just found an answer, from which works after some fiddling.
If one ammends the link YH provides from
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
to
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/drive.readonly&response_type=code
then the resulting access token works with the websave command in my question, so long as the client id and secret are from the credential type 'Desktop', as created in the google developer console. I had previously tried this approach, but with the credential type 'TV and limited input', as that was the only type that worked with the RunOnce function in the matlab2sheets FeX submission (n.b. the readme for that function says to use the 'other' type, which is now unavaliable). One can save the acces_token as a .mat for future use.

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

답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by