list google drive files with curl

前端 未结 2 2036
春和景丽
春和景丽 2020-12-15 02:06

I\'m trying to get the list of files on Google Drive with curl, but OAuth 2 is getting the best of me.

Here are some of the things I tried:

curl -H \         


        
2条回答
  •  死守一世寂寞
    2020-12-15 02:35

    For future googlers:

    If you want to save yourself an afternoon of pain, forget google's doumentation and head over here

    The gist of it, since I know stackoverflow prefers quoting the content to linking:

    First, get your client ID in Google Developer Console for OAuth2. This will require to fill in the OAuth consent screen and after testing, you will have to get your domain verified. (Google will lead you in the process...)

    In your browser:

    https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=1234567890.apps.googleusercontent.com
    

    Allow the access, of course, and copy the code which should look like 4/v6xr77ewYqjkslsdUOKwAzu

    curl -H "Content-Type: application/x-www-form-urlencoded" -d 'code=4/v6xr77ewYqjkslsdUOKwAzu&client_id=1234567890.apps.googleusercontent.com&client_secret=xywzxywzxywzxywzxywz&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code' https://accounts.google.com/o/oauth2/token
    

    You’ll get a JSON like this one:

    {
      "access_token" : "ya29.AHES6Zkjhkjhahskjhskkskjh",
      "token_type" : "Bearer",
      "expires_in" : 3600,
      "refresh_token" : "1/HH9E7k5D0jakjhsd7askdjh7899a8sd989"
    }
    

    If you curl:

    curl 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ya29.AHES6Zkjhkjhahskjhskkskjh'
    

    you’ll get something like:

    {
     "issued_to": "562211803675.apps.googleusercontent.com",
     "audience": "562211803675.apps.googleusercontent.com",
     "scope": "https://www.googleapis.com/auth/analytics",
     "expires_in": 3556
    }
    

    Done

    curl 'https://www.googleapis.com/analytics/v3/management/accounts?access_token=ya29.AHES6Zkjhkjhahskjhskkskjh
    

    Renew the token

    You have to use the “refresh_token” received previously

    curl -d "client_id=562211803675.apps.googleusercontent.com&client_secret=ZQxoOBGbvMGnZOYUrVIDXrgl&refresh_token=1/HH9E7k5D0jakjhsd7askdjh7899a8sd989&grant_type=refresh_token" https://accounts.google.com/o/oauth2/token
    

    and you’ll get a new access_token.

提交回复
热议问题