Search code examples
google-oauthgoogle-drive-api

How do I access Google Drive Application Data from a remote server?


For my application I want the user to be able to store files on Google Drive and for my service to have access to these same files created with the application.

I created a Client ID for web application and was able to upload/list/download files from JavaScript (client side) with drive.appfolder scope. This is good, this is half of what I want to do.

Now I want to access the same files from Node.js (server side). I am lost as to how to do this. Do I create a new Client ID for the server? (if so, how will the user authenticate?) Do I pass the AuthToken my user got client-side and try to use that on the server? I don't think this will work as the AuthToke is time-sensitive (and probably not intended to be used from multiple IPs).

Any direction or example server-side code will be helpful. Again, all I want is to access these same files the user created with my application, not any other files in the user's Google Drive.

CLARIFICATION: I think my question boils down to: "Is it possible to access the same Application Data on Google Drive both client-side and server-side?"


Solution

  • Do I create a new Client ID for the server?

    Up to you. You don't need to, but you can. See below.

    if so, how will the user authenticate?

    Up to you. OAuth is about authorisation, not authentication. Just in case you meant authorisation, the user authorises the Project, which may contain multiple client IDs.

    Do I pass the AuthToken my user got client-side and try to use that on the server?

    You can do, but not a good idea for the reason you state. The preferred approach is to have a separate server Client ID, and use that to request offline access, which returns (eventually) a Refresh Token, which you store in your server. You then use that Refresh Token to request Access Tokens whenever you need them.

    AuthToken is ... (and probably not intended to be used from multiple IPs).

    It is not bound to a specific IP address

    Is it possible to access the same Application Data on Google Drive both client-side and server-side?"

    Yes

    Most of what you need is at https://developers.google.com/accounts/docs/OAuth2WebServer