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

Usage on AWS Lambda with Oauth #37

Open
andrewdoro opened this issue Nov 14, 2024 · 6 comments
Open

Usage on AWS Lambda with Oauth #37

andrewdoro opened this issue Nov 14, 2024 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@andrewdoro
Copy link

Describe the bug

I am trying to use this library on AWS Lambda, serverless. I've added the basic oauth configuration. It worked for an hour and now I am getting this error. "Failed to refresh access token" is this happening because of a wrong refresh token value ?. I just copied the one from the generate-own script.

import { YtdlCore } from '@ybd-project/ytdl-core/serverless';


const oauth2Credentials = {
  accessToken: Resource.YOUTUBE_ACCESS_TOKEN.value,
  refreshToken: Resource.YOUTUBE_REFRESH_TOKEN.value,
  expiryDate: Resource.YOUTUBE_EXPIRY_DATE.value,
  clientData: {
    clientId: Resource.YOUTUBE_CLIENT_ID.value,
    clientSecret: Resource.YOUTUBE_CLIENT_SECRET.value,
  },
};

  const ytld = new YtdlCore({
      quality: 'lowestaudio',
      filter: 'audioonly',
      hl: 'en',
      gl: 'US',

      disableDefaultClients: true,
      disablePoTokenAutoGeneration: true,
      disableInitialSetup: true,
      parsesHLSFormat: false,
      noUpdate: true,
      logDisplay: ['warning', 'error'],
      clients: ['mweb', 'web'],

      oauth2Credentials: oauth2Credentials,
      html5Player: {
        useRetrievedFunctionsFromGithub: true,
      },
    });

    const info = await ytld.getBasicInfo(event.url, { oauth2Credentials });

    if (info.videoDetails.lengthSeconds > 3600) {
      // 1 hour limit
      throw new Error('Video is too long. Maximum duration is 1 hour.');
    }

    const stream = await ytld.download(event.url, { oauth2Credentials });

Error Details (Log)

[ ERROR ]: Failed to refresh access token: 401

Environment

@andrewdoro andrewdoro added the bug Something isn't working label Nov 14, 2024
@andrewdoro
Copy link
Author

It was a wrong environment variable on my side. It's working now. Also I was running the refresh token script and I got this message. Also because I am using serverless is the new accessToken save in any place, or is there an option to do that. Right now I think it will always use the refresh token.

| Important: The uniquely generated tokens may become unavailable if they are not updated for 2~3 days.

Does this mean that the refresh token can become expired?

@ybd-project
Copy link
Owner

ybd-project commented Nov 15, 2024

| Important: The uniquely generated tokens may become unavailable if they are not updated for 2~3 days.

Does this mean that the refresh token can become expired?

Normally, tokens that are generated independently should be refreshed every day or so due to their short expiration time. Although we have not experimented in detail, we have determined this because the value of expiryDate is one day later.

@ybd-project
Copy link
Owner

It was a wrong environment variable on my side. It's working now. Also I was running the refresh token script and I got this message. Also because I am using serverless is the new accessToken save in any place, or is there an option to do that. Right now I think it will always use the refresh token.

If you are using serverless, the cache would be placed in the temp folder. If you need the ability to retrieve it, this will be added in v6.0.9.

@ybd-project ybd-project self-assigned this Nov 15, 2024
@andrewdoro
Copy link
Author

Yeah the idea, is to be able to get the new tokens, and save them in a bucket/db. On serverless, the values from the temp folder won't be persisted.

Thank you for your work

@andrewdoro
Copy link
Author

But it's still kinda painful that I have to redo the oauth sign in flow again when the refresh token expires. Are there any other solution that don't require manual handling of these. My use case is to just download a video when the transcript is not available and process it's audio with Whisper.

@ybd-project
Copy link
Owner

In case you are wondering, OAuth2 tokens are automatically renewed when they expire (after the expiryDate date and time).
Since the cache (temporary) of the temp folder is not valid for serverless, should users be able to specify their own caching mechanism?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants