How to use a refresh_token for youtube python api?
<p>So i got a refresh token in this way and can I keep it?</p>

<p>And if so, how do I use it next time, so that there is no need for me to open browser?</p>

<p>Right now I'm thinking about creating OAuth2Credentials object directly, is this the right way?</p>

<pre><code>from urllib.parse import urlparse, parse_qs
from oauth2client.client import flow_from_clientsecrets, OAuth2Credentials
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.contrib import gce
import httplib2
import webbrowser

CLIENT_SECRETS_FILE = "bot_credentials.json"
flow = client.flow_from_clientsecrets(
     CLIENT_SECRETS_FILE,
    scope=scope,
redirect_uri='http://127.0.0.1:65010')
flow.params['include_granted_scopes'] = 'true'
flow.params['access_type'] = 'offline'
auth_uri = flow.step1_get_authorize_url()
webbrowser.open(auth_uri)
url = input('Please enter the redirected url with code')
code = get_url_param(url, 'code')
if code is None:
    print('there is an error in your redirect link with code parameter, check if it exists')
    exit()
print(code)
credentials = flow.step2_exchange(code[0])
print(credentials.to_json())#refresh_token here!!!
</code></pre>

<p>If the user consents to authorize your application to access those resources, Google will return a token to your application. Depending on your application's type, it will either validate the token or exchange it for a different type of token. Check this <a href="https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2" rel="nofollow">documentation</a>.</p>

<blockquote>
  <p>For example, a server-side web application would exchange the returned token for an access token and a <strong>refresh token</strong>. The access token would let the application authorize requests on the user's behalf, and <strong>the refresh token would let the application retrieve a new access token when the original access token expires.</strong></p>
</blockquote>

<p>Basically, if your application obtains a refresh token during the authorization process, then you will need to periodically use that token to obtain a new, valid access token. Server-side web applications, installed applications, and devices all obtain refresh tokens. </p>

<p>It is stated <a href="https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Refreshing_a_Token" rel="nofollow">here</a> that at any time, your application can send a POST request to Google's authorization server that specifies your client ID, your client secret, and the <strong>refresh token</strong> for the user. The request should also set the <code>grant_type</code> parameter value to <code>refresh_token</code>.</p>

<blockquote>
  <p>The following example demonstrates this request:</p>

<pre><code>POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

client_id=21302922996.apps.googleusercontent.com&amp;
client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&amp;
refresh_token=1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ&amp;
grant_type=refresh_token
</code></pre>
  
  <p>The authorization server will return a JSON object that contains a new access token:</p>

<pre><code>{
  "access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
  "expires_in":3920,
  "token_type":"Bearer"
}
</code></pre>
</blockquote>

<p>You can check this <a href="https://gist.github.com/ikai/5905078" rel="nofollow">sample on GitHub</a>  to generate a refresh token for the YouTube API. Note that this will will also create a file called <code>generate_token.py-oauth</code> that contains this information.</p>

python youtube-api google-api-client