Deye

API Command (Google)

26 posts in this topic

#1 ·  Posted (edited)

Hi,
Can anyone give me an idea how a command\function needs to look like for using this command
https://developers.google.com/drive/v2/reference/files/touch
If It will be used  to touch a file in my Google drive account say for every 5 seconds with a REST API key from my side, And make it work so on the other side where the file's date is to be acquired (and (?) How can it be compared accordingly to the servers time to see when it was last modified like if  =< 5 sseconds then ..)
I need this to have some way to communicate with the client's side, to tell if my side is reachable, so if my side didn't touch the given file for over 10 seconds, that means that my computer is off due to power failure or something like that
If this first verification passes then I can proceed the communication through emailing 
I tried to look in to TCP server client examples but didn't manage to get the port forwarding to work which stopped my progress .Etc (no experience ..) so I was thinking this method may suit my needs and will be more secure anyways
Thanks

Edited by Deye

Share this post


Link to post
Share on other sites



#2 ·  Posted

This is http post request with OAuth 2.0 authorization.

Share this post


Link to post
Share on other sites

#3 ·  Posted

Last calling, Any votes to how I may pass the above command with an api key.. 
Thanks again

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

yes,  I already created OAuth 2.0 client IDs  even have the JSON file created and downloaded if needed 

and I have the file's ID let me know what else is needed

Thanks again

Edited by Deye

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

21 hours ago, Deye said:

yes,  I already created OAuth 2.0 client IDs  even have the JSON file created and downloaded if needed 

and I have the file's ID let me know what else is needed

Thanks again

@Deye download this udf https://github.com/inververs/googletoken

This is example with  OAuth 2.0 client ID. But it is better for you to make a service account

have no google drive. Therefore, I can not say what the result will be.

Set your own json file and file id

#include '_googletoken.au3'
$client_json_file = 'client_secret_878378520961-hehpvlick1fkk1f24f1kbhqnl4vep6so.apps.googleusercontent.com.json'
_googleapis_setupDesktopOAuth2FromFile($client_json_file)
_googleapis_setScope('https://www.googleapis.com/auth/drive')
$sAccessToken = _googleapis_getToken()
MsgBox(0, 'Token', $sAccessToken)
If Not $sAccessToken Then Exit

$sFileId = 'enter here fileid'
$sURL = 'https://www.googleapis.com/drive/v2/files/' & $sFileId & '/touch'
$sHead = 'Authorization=' & 'Bearer ' & $sAccessToken
$sResponse = __googleapis_request('POST', $sURL, '', $sHead)
MsgBox(0, 'Response', $sResponse)
Edited by Inververs
2 people like this

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

If you create Service account keys then replace 

_googleapis_setupDesktopOAuth2FromFile

to

_googleapis_setupServiceOAuth2FromFile

Edited by Inververs

Share this post


Link to post
Share on other sites

#8 ·  Posted

 Thanks 
 I get errors from JSMN.au3 Scite syntax errors because it is in HTML code I'm guessing.. 

didn't yet look in to  Service account ..will check it out now

Share this post


Link to post
Share on other sites

#9 ·  Posted

No error there. I do not know what your version of autoit, I use 3.3.12.0

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

 Sorry, My bad I downloaded instead of coping the files contents 
 
 So I Just tested this out:
 I see it will bring me to the Google choose account page, meaning I need to approve a token each time I run the script, is there anyway around this ?
 
 anyhow with my current test the file isn't seen as touched in the activity panel, will look into how to configure the Service account ..
 
 Thanks again

Edited by Deye

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

16 minutes ago, Deye said:

I see it will bring me to the Google choose account page, meaning I need to approve a token each time I run the script, is there anyway around this ?

It's strange that you need to confirm the token every start. With this authorization method, the token will be valid for an hour, then it will be updated via a refresh token and only after Google refuses to accept the refresh token, it will again require permission.

But if you work with your account, then you need to do the service key. Then Google will not ask for confirmation at all.

When I first made a request to 'https://www.googleapis.com/drive/v2/files/' & $sFileId & '/touch'  Google asked me to further customize  my application to work with GoogleDrive.

Edited by Inververs

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Edit: keep you posted ..

 Sorry if my responses are delayed my router keeps hanging up my connection, needs to be replaced by my ISP tomorrow ..

Edit: 

This time I used _googleapis_setupServiceOAuth2FromFile($client_json_file) ;service key
 And changed the $client_json_file reference but it seems the AccessToken isn't getting acquired
 will be back to this later, maybe I missed something ..
 Thanks again for sharing this wonderful UDF

Edited by Deye

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

How it works in my account

#include 'C:\AutoIt\googletoken\_googletoken.au3'

$service_json_file = 'saaaass-33d6b60d19d1.json'
_googleapis_setupServiceOAuth2FromFile($service_json_file)

_googleapis_setScope('https://www.googleapis.com/auth/drive')
$sAccessToken = _googleapis_getToken()

$sFileId = '0ByJi1lVsD3aNMVZybUNocHlaa1k'
$sURL = 'https://www.googleapis.com/drive/v2/files/' & $sFileId & '/touch'
$sHead = 'Authorization=' & 'Bearer ' & $sAccessToken
$sResponse = __googleapis_request('POST', $sURL, '', $sHead)
ConsoleWrite($sResponse & @CRLF)
MsgBox(0, 'Response', $sResponse)
{
 "kind": "drive#file",
 "id": "0ByJi1lVsD3aNMVZybUNocHlaa1k",
 "etag": "\"dUcwyQ6hN8QZTeM4-DHEZmgLfsU/MTUwMDI5MDExNTQ1MA\"",
 "selfLink": "https://www.googleapis.com/drive/v2/files/0ByJi1lVsD3aNMVZybUNocHlaa1k",
 "webContentLink": "https://drive.google.com/uc?id=0ByJi1lVsD3aNMVZybUNocHlaa1k&export=download",
 "alternateLink": "https://drive.google.com/file/d/0ByJi1lVsD3aNMVZybUNocHlaa1k/view?usp=drivesdk",
 "embedLink": "https://drive.google.com/file/d/0ByJi1lVsD3aNMVZybUNocHlaa1k/preview?usp=drivesdk",
 "iconLink": "https://drive-thirdparty.googleusercontent.com/16/type/application/octet-stream",
 "title": "Nothhing.au3",
 "mimeType": "application/octet-stream",
 "labels": {
  "starred": false,
  "hidden": true,
  "trashed": false,
  "restricted": false,
  "viewed": true
 },
 "createdDate": "2012-06-10T15:10:54.871Z",
 "modifiedDate": "2017-07-17T11:15:15.450Z",
 "modifiedByMeDate": "2017-07-17T11:15:15.450Z",
 "lastViewedByMeDate": "2017-07-17T11:15:27.029Z",
 "markedViewedByMeDate": "1970-01-01T00:00:00.000Z",
 "version": "29214",
 "parents": [
  {
   "kind": "drive#parentReference",
   "id": "0ByJi1lVsD3aNa2tSN25acWNaajA",
   "selfLink": "https://www.googleapis.com/drive/v2/files/0ByJi1lVsD3aNMVZybUNocHlaa1k/parents/0ByJi1lVsD3aNa2tSN25acWNaajA",
   "parentLink": "https://www.googleapis.com/drive/v2/files/0ByJi1lVsD3aNa2tSN25acWNaajA",
   "isRoot": false
  }
 ],
 "downloadUrl": "https://doc-0c-4s-docs.googleusercontent.com/docs/securesc/a3dnmh0b370lmkfcj5cn6h7cs32bueo7/t2g77uuf0pk5bdr4m8uqctctjnri359p/1500285600000/03966103873089652193/03966103873089652193/0ByJi1lVsD3aNMVZybUNocHlaa1k?e=download&gd=true",
 "userPermission": {
  "kind": "drive#permission",
  "etag": "\"dUcwyQ6hN8QZTeM4-DHEZmgLfsU/lAILIw_X_2WHGaSLGP8HDNI4aNI\"",
  "id": "me",
  "selfLink": "https://www.googleapis.com/drive/v2/files/0ByJi1lVsD3aNMVZybUNocHlaa1k/permissions/me",
  "role": "owner",
  "type": "user"
 },
 "originalFilename": "Nothhing.au3",
 "fileExtension": "au3",
 "md5Checksum": "fac403b6e6831b9c079f8cb4632a94e2",
 "fileSize": "463",
 "quotaBytesUsed": "463",
 "ownerNames": [
  "#############"
 ],
 "owners": [
  {
   "kind": "drive#user",
   "displayName": "#############",
   "isAuthenticatedUser": true,
   "permissionId": "03966103873089652193",
   "emailAddress": "#############"
  }
 ],
 "lastModifyingUserName": "#############",
 "lastModifyingUser": {
  "kind": "drive#user",
  "displayName": "#############",
  "isAuthenticatedUser": true,
  "permissionId": "03966103873089652193",
  "emailAddress": "#############"
 },
 "capabilities": {
  "canCopy": true,
  "canEdit": true
 },
 "editable": true,
 "copyable": true,
 "writersCanShare": true,
 "shared": false,
 "explicitlyTrashed": false,
 "appDataContents": false,
 "headRevisionId": "0ByJi1lVsD3aNcVNXeCtnSHlvN0xLNllxdzF2Mzl4Z0RoalRZPQ",
 "spaces": [
  "drive"
 ]
}

 

Edited by Inververs

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

  Sorry, I had to jump out
  just tried, not sure why I keep getting this error
 

 "_googletoken.au3" (1004) : ==> The requested action with this object has failed.:
$oHttp.SetRequestHeader($head, $default_headers.item($head))

Edit: NM, I recreated the service account, now I need to check why the AccessToken isn't getting acquired
Will do some more test, not an intuitive setup as thought it would be  

Edited by Deye

Share this post


Link to post
Share on other sites

#15 ·  Posted

Which version of autoit do you use? And which is operating system?

Share this post


Link to post
Share on other sites

#16 ·  Posted

3.3.14.2
WIN_7 64

I keep the .json file in @ScriptDir

The return from _googleapis_getToken()

@error = 57005
@extended = 48879

Thanks

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

If you get COM errors, such as 

The requested action with this object has failed

This means that the version of the autoit does not fit. See bug tracker If you are interested why.

Ok, it look like you dont know how to check errors. Lets try:

#include '_googletoken.au3'

$service_json_file = 'CardRegistration-33d6b60d19d1.json'
_googleapis_setupServiceOAuth2FromFile($service_json_file)
If @error Then Exit MsgBox(16, '_googleapis_setupServiceOAuth2FromFile', @error)

_googleapis_setScope('https://www.googleapis.com/auth/drive')
If @error Then Exit MsgBox(16, '_googleapis_setScope', @error)

$sAccessToken = _googleapis_getToken()
If @error Then Exit MsgBox(16, '_googleapis_getToken', @error)


$sFileId = '0ByJi1lVsD3aNMVZybUNocHlaa1k'
$sURL = 'https://www.googleapis.com/drive/v2/files/' & $sFileId & '/touch'
$sHead = 'Authorization=' & 'Bearer ' & $sAccessToken
$sResponse = __googleapis_request('POST', $sURL, '', $sHead)
If @error Then Exit MsgBox(16, '__googleapis_request', @error)

MsgBox(0, 'Response', $sResponse)

 

Edited by Inververs

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

it goes strait to

_googletoken.au3" (1004) : ==> The requested action with this object has failed.:
$oHttp.SetRequestHeader($head, $default_headers.item($head))
$oHttp^ ERROR

if I move .json to another place i Get error 1 _googleapis_setupServiceOAuth2FromFile

Edited by Deye

Share this post


Link to post
Share on other sites

#19 ·  Posted

lol, brevity is the soul of wit.

Forgive me, dude, but your answers do not help me. Understand yourself.

What's error 1? In what place, I wrote you an MSGbox, it's heading ... and you're just sending me error number 1, are you serious?

1 person likes this

Share this post


Link to post
Share on other sites

#20 ·  Posted

just  before you posted ..edited

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now