Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run headless by making it load from created credential json file #395

Closed
mikebilly opened this issue Oct 23, 2023 · 8 comments
Closed

Run headless by making it load from created credential json file #395

mikebilly opened this issue Oct 23, 2023 · 8 comments
Assignees
Labels
wontfix This will not be worked on

Comments

@mikebilly
Copy link

I'm trying to upload medias from mounted Google Drive on Google Colab, this is my code to save the credential file:

from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client import tools
from googleapiclient.discovery import build

SCOPES = ['https://www.googleapis.com/auth/photoslibrary.appendonly']
API_SERVICE_NAME = 'photoslibrary'
API_VERSION = 'v1'

def filename(path):
  return os.path.basename(path)

def get_authenticated_service(client_secret_file):
  client_secret_name, _ext = os.path.splitext(filename(client_secret_file))
  if not os.path.exists("/content/drive/MyDrive/gphotos-uploader"):
     os.makedirs("/content/drive/MyDrive/gphotos-uploader")
     os.makedirs("/content/drive/MyDrive/gphotos-uploader/credentials")

  folder_containing_credential = "/content/drive/MyDrive/gphotos-uploader/credentials/" + client_secret_name
  if not os.path.exists(folder_containing_credential):
    os.makedirs(folder_containing_credential)
    print("making folder containing credentials for {}".format(client_secret_name))
  else:
    print("folder containing credentials for {} exists".format(client_secret_name))
    
  credential_json = folder_containing_credential + "/" + "credentials_for_" + client_secret_name + ".json"
  storage = Storage(credential_json)
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    flow = flow_from_clientsecrets(client_secret_file, SCOPES)
    flags = tools.argparser.parse_args(args=['--noauth_local_webserver'])
    credentials = tools.run_flow(flow, storage, flags)

  return build(API_SERVICE_NAME, API_VERSION, credentials = credentials, static_discovery=False)

and then run:

client_secret_file = '/content/1.json'
service = get_authenticated_service(client_secret_file)

this code creates the credential json file, for example, it creates: /content/drive/MyDrive/gphotos-uploader/credentials/1/credentials_for_1.json in my case.

how to make gphotos-uploader-cli use my credentials_for_1.json which looks like this:

{
  "access_token": "ya29.xxxxxx-xxxxx-xxxxx-xxxxxxxxxx",
  "client_id": "xxxxxx-xxxxxxxxxxxx.apps.googleusercontent.com",
  "client_secret": "xxxxxxxx-xxxxxxxxxxxxxxx",
  "refresh_token": "1//xxxxxxxxx-xxxxxxxxxxxxxx-xxxxxxxxxx",
  "token_expiry": "2023-10-23T03:20:51Z",
  "token_uri": "https://oauth2.googleapis.com/token",
  "user_agent": null,
  "revoke_uri": "https://oauth2.googleapis.com/revoke",
  "id_token": null,
  "id_token_jwt": null,
  "token_response": {
    "access_token": "ya29.xxxxxx-xxxxx-xxxxx-xxxxxxxxxx",
    "expires_in": 3599,
    "refresh_token": "1//xxxxxxxxx-xxxxxxxxxxxxxx-xxxxxxxxxx",
    "scope": "https://www.googleapis.com/auth/photoslibrary.appendonly",
    "token_type": "Bearer"
  },
  "scopes": [
    "https://www.googleapis.com/auth/photoslibrary.appendonly"
  ],
  "token_info_uri": "https://oauth2.googleapis.com/tokeninfo",
  "invalid": false,
  "_class": "OAuth2Credentials",
  "_module": "oauth2client.client"
}
@pacoorozco
Copy link
Member

Hi,

Your code is using the OAtuh 2.0 to get the permissions to use Google Photos API. The gphotos-uploader-cli does exactly the same, so maybe you can use the client-id and the client-secret from above to configure the gphotos-uploader-cli. This is true if you have created a project were the CLI is able to use the Google Photos API.

My best suggestion is to follow the Getting started documentation.

Let me know if there's a problem when using these credentials.

@pacoorozco pacoorozco added the fixed The bug has been fixed, but the fix is not permanent. Needs a refactor. label Oct 23, 2023
@pacoorozco pacoorozco self-assigned this Oct 23, 2023
@mikebilly
Copy link
Author

mikebilly commented Oct 23, 2023

I can use the above code: get_authenticated_service(client_secret_file) to create the credentials json file from a client secret json file. But how to make gphotos-uploader-cli get the token from that created credentials json file, i.e /content/drive/MyDrive/gphotos-uploader/credentials/1/credentials_for_1.json, instead of asking the user to go to a local host link which doesn't work on Google Colab.

@pacoorozco
Copy link
Member

Currently, gphotos-uploader-cli doesn't offer a way to use external credentials (like the one you're using).

I can't imagine how to re-use the credentials of another program to authenticate gphotos-uploader-cli.

@pacoorozco pacoorozco removed the fixed The bug has been fixed, but the fix is not permanent. Needs a refactor. label Oct 23, 2023
@mikebilly
Copy link
Author

Not of another problem, that function is specifically used to get the token for gphotos-uploader-cli, because I want to try to make it get the token without going to localhost link. Got the credentials, now just need gphotos-uploader-cli to get the token from it.

@mikebilly
Copy link
Author

@pacoorozco could you help me with this?, could you please try gphotos-uploader-cli on Google Colab, I'm sure there's a demand to upload medias from Google Drive to Google Photos through Google Colab.

@pacoorozco
Copy link
Member

Hi @mikebilly

AFAIK Google Colab is a python environment to set up Notebooks and run python scripts. I've never used it, so I don't know how it should work in order to run a go CLI, like gphotos-uploader-cli.

If you were able to mount the Google Drive to a folder, then you can use gphotos-uploader-cli to upload the content of the folder.

@mikebilly
Copy link
Author

@pacoorozco it's a headless environment, which means doing authentication is very challenging as it can't access localhost. Still looking for a way to make gphotos-uploader-cli use the pre-created credentials to retrieve the token or find other ways to get the token without needing localhost.

@pacoorozco
Copy link
Member

I understand the complexity of running this script in a headless environment. In fact, this script was never thought to be run in a headless setup, because the authentication is not easy to be implemented.

In Google Colab they are taking advantage of the fact that their service is in the Google scope 😅

@pacoorozco pacoorozco added the wontfix This will not be worked on label Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants