Yoav Keren
Yoav Keren

Reputation: 99

Youtube API - terminal attempt to open the browser and fails

I'm trying to build a project using Youtube's API and Python3. As mentioned in the Quick Start guide:

The sample attempts to open a new window or tab in your default browser. If this fails, copy the URL from the console and manually open it in your browser.

I'm using MacOS Terminal which runs the script but I really do need to copy the URL into my browser.

I guess the problem is in my machine, and I'd like to find a solution how to fix it, as it would be faster and easier, each and every time I run the script.

I've tried to find similar thread, with no luck. If anyone can guide my through, or send me a link, for how to solve this problem.

Thanks, Yoav.

Upvotes: 0

Views: 419

Answers (2)

Yoav Keren
Yoav Keren

Reputation: 99

Solution (source):

I've used the run_console() which don't attempt to run the browser, but ask for the client to open it manually. To make it run the browser automatically, you should use run_local_server() method as shown in the example below.

The run_console function instructs the user to open the authorization URL in their browser. After the user authorizes the application, the authorization server displays a web page with an authorization code, which the user then pastes into the application. The authorization library automatically exchanges the code for an access token.

credentials = flow.run_console()

The run_local_server function attempts to open the authorization URL in the user's browser. It also starts a local web server to listen for the authorization response. After the user completes the auth flow, the authorization server redirects the user's browser to the local web server. That server gets the authorization code from the browser and shuts down, then exchanges the code for an access token.

credentials = flow.run_local_server(host='localhost',
>     port=8080, 
>     authorization_prompt_message='Please visit this URL: {url}', 
>     success_message='The auth flow is complete; you may close this window.',
>     open_browser=True)

Thank you @Hassan Voyeau for the help.

Upvotes: 1

Hassan Voyeau
Hassan Voyeau

Reputation: 3624

You need a BROWSER environment variable set. This points to the location of the browser.

Use getenv BROWSER to see if it is already set

*Command may be different depending on version of Mac OS

Upvotes: 1

Related Questions