1

So I am trying to create a Discord bot that can create and update playlists with the youtube API. However when I go to create the playlist it say's i need to visit a URL to authorize this application, eg.

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=523005278110-dudud40lafimhbq73h9kcamc39g14eqv.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.force-ssl&state=MCgahMYze4U8Tgx35u2TGGjPdXg3fZ&prompt=consent&access_type=offline

How would I make it so this doesnt happen, and my bot has access to the account to create playlists whenever it needs to?

//client_secrets has client_id and client_secret
CLIENT_SECRETS_FILE = 'client_secret.json'
SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']
API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'

//This should authorize me but isn't or i've done it wrong
def get_authenticated_service():
  flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
  credentials = flow.run_console()
  return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)

//This creates the playlist
def add_playlist(youtube, args):

  body = dict(
    snippet=dict(
      title=args[0],
      description=args[1]
    ),
    status=dict(
      privacyStatus='public'
    ) 
  ) 

  playlists_insert_response = youtube.playlists().insert(
    part='snippet,status',
    body=body
  ).execute()

  print('New playlist ID: %s' % playlists_insert_response['id'])

//This is the discord.py code that is called, and then calls the playlist creation code.
class SimpleCog:
    """SimpleCog"""

    def __init__(self, bot):
        self.bot = bot

    @commands.command(name='newPlaylist', aliases=['np','new'])
    @commands.guild_only()
    async def newPlaylist(self, ctx, *, operand: str):
        playlistName=operand.split()[0]
        playlistDesc = "Playlist created by {0.name}#{0.discriminator} for server {1}".format(ctx.author,ctx.guild)
        args = [playlistName,playlistDesc]
        youtube = get_authenticated_service()
        try:
          add_playlist(youtube, args)
        except HttpError as e:
          await ctx.send('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content))
  • 2
    https://stackoverflow.com/questions/31261604/youtube-data-api-how-to-avoid-google-oauth-redirect-url-authorization This may be a useful answer – Tim Aug 07 '18 at 18:44
  • Im not sure how that would help me. – Ed Miliband Aug 07 '18 at 19:03
  • The main part of that answer is that you need to follow the link once and get it set up from it. Currently as far as I can see, all you code is doing is returning this link which you need to follow a setup. Follow the steps in the google OAuth 2 documnetation to set it up fully. https://developers.google.com/api-client-library/python/guide/aaa_oauth – Tim Aug 07 '18 at 20:54
  • That worked, hadn't seen that page before. Thanks! – Ed Miliband Aug 07 '18 at 21:34

0 Answers0