Skip to content Skip to sidebar Skip to footer

Any Way To Use Google Api Without Every-time Authentication?

I tried to use the API on python in a autorun on a PC. But I can't because every time the program starts, it asks me about the authorization code. This is my code: client_secret_fi

Solution 1:

Becouse you are using the YouTube API you can not use a service account you will need to use Oauth2.

Oauth2 can return something called a refresh token, if you store this token your code can then access the refresh token the next time it runs and use the refresh token to request a new access token this way it will not need to ask you every time it runs to access your data.

# The file token.pickle stores the user's access and refresh tokens, and is# created automatically when the authorization flow completes for the first# time.if os.path.exists('token.pickle'):
    withopen('token.pickle', 'rb') as token:
        creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.ifnot creds ornot creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file(
            'credentials.json', SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next runwithopen('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

The only offical sample that includes refresh token that i know if is here Python quickstart you will need to alter it for YouTube but the auth code is really all you need just plug that int your code and you should be GTG

Solution 2:

You can achieve this using Service Account.

https://googleapis.dev/python/google-api-core/latest/auth.html#service-accounts

Service account are technically pre authorized by the developer.

IF the API does not support Service account( in some cases) then you can try oauth2 where you have a consent for asking the owner of the account if you can access

Post a Comment for "Any Way To Use Google Api Without Every-time Authentication?"