How to use the oauth2client.client.OAuth2WebServerFlow function in oauth2client

To help you get started, we’ve selected a few oauth2client examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github googleapis / oauth2client / tests / test_client.py View on Github external
def test_scope_is_required(self):
        with self.assertRaises(TypeError):
            client.OAuth2WebServerFlow('client_id+1')
github TobiasTheViking / googledriveannex / googledriveannex.py View on Github external
def login():
    common.log("")
    global service, http
    if os.path.exists(pwd + "/googledriveannex.creds"):
        common.log("Loading credentials")
        storage = Storage(pwd + "/googledriveannex.creds")
        credentials = storage.get()
    else:
        flow = OAuth2WebServerFlow(client_id, client_secret, oauth_scope, redirect_uri)
        authorize_url = flow.step1_get_authorize_url()
        print 'Go to the following link in your browser: ' + authorize_url
        code = raw_input('Enter verification code: ').strip()
        credentials = flow.step2_exchange(code)

        common.log("Saving credentials")
        storage = Storage(pwd + "/googledriveannex.creds")
        storage.put(credentials)

    http = credentials.authorize(http)
    common.log("Done: " + repr(credentials) + " - " + repr(storage))
    service = build('drive', 'v2', http=http)
github jay0lee / got-your-back / oauth2client / contrib / django_util / views.py View on Github external
to the path of the current request.

    Returns:
        An OAuth2 flow object that has been stored in the session.
    """
    # Generate a CSRF token to prevent malicious requests.
    csrf_token = hashlib.sha256(os.urandom(1024)).hexdigest()

    request.session[_CSRF_KEY] = csrf_token

    state = json.dumps({
        'csrf_token': csrf_token,
        'return_url': return_url,
    })

    flow = client.OAuth2WebServerFlow(
        client_id=django_util.oauth2_settings.client_id,
        client_secret=django_util.oauth2_settings.client_secret,
        scope=scopes,
        state=state,
        redirect_uri=request.build_absolute_uri(
            urlresolvers.reverse("google_oauth:callback")))

    flow_key = _FLOW_KEY.format(csrf_token)
    request.session[flow_key] = jsonpickle.encode(flow)
    return flow
github embr / gcat / gcat / __init__.py View on Github external
def get_service(opts):
    flow = OAuth2WebServerFlow(client_id=opts['client_id'],
                               client_secret=opts['client_secret'],
                               scope=opts['scope'],
                               redirect_uri=opts['redirect_uri'])

    credentials = get_credentials(flow, opts)

    http = httplib2.Http()
    http = credentials.authorize(http)

    service = build('drive', 'v2', http=http)
    return service
github google / hyou / tools / generate_oauth2_credentials.py View on Github external
def main(argv):
    parser = create_parser()
    opts = parser.parse_args(argv[1:])

    flow = oauth2client.client.OAuth2WebServerFlow(
        client_id=opts.client_id,
        client_secret=opts.client_secret,
        scope=hyou.SCOPES)
    url = flow.step1_get_authorize_url('urn:ietf:wg:oauth:2.0:oob')

    print()
    print('Please visit this URL to get the authorization code:')
    print(url)
    print()

    code = py3.input('Code: ').strip()

    credentials = flow.step2_exchange(code)

    with py3.open(opts.output_json_path, 'wb') as f:
        os.fchmod(f.fileno(), 0o600)
github anticodeninja / michel2 / michel / michel.py View on Github external
def get_service(profile_name):
    """
    Handle oauth's shit (copy-pasta from
    http://code.google.com/apis/tasks/v1/using.html)
    Yes I do publish a secret key here, apparently it is normal
    http://stackoverflow.com/questions/7274554/why-google-native-oauth2-flow-require-client-secret
    """
    storage = oauth2client.file.Storage(save_data_path("oauth.dat"))
    credentials = storage.get()
    if not credentials or credentials.invalid:
        flow = client.OAuth2WebServerFlow(
            client_id='617841371351.apps.googleusercontent.com',
            client_secret='_HVmphe0rqwxqSR8523M6g_g',
            scope='https://www.googleapis.com/auth/tasks',
            user_agent='michel/0.0.1')
        flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args("")
        credentials = tools.run_flow(flow, storage, flags)
    http = httplib2.Http()
    http = credentials.authorize(http)
    return discovery.build(serviceName='tasks', version='v1', http=http)
github fniephaus / alfred-gmail / src / oauth2client / contrib / flask_util.py View on Github external
csrf_token = hashlib.sha256(os.urandom(1024)).hexdigest()

        session[_CSRF_KEY] = csrf_token

        state = json.dumps({
            'csrf_token': csrf_token,
            'return_url': return_url
        })

        kw = self.flow_kwargs.copy()
        kw.update(kwargs)

        extra_scopes = kw.pop('scopes', [])
        scopes = set(self.scopes).union(set(extra_scopes))

        flow = client.OAuth2WebServerFlow(
            client_id=self.client_id,
            client_secret=self.client_secret,
            scope=scopes,
            state=state,
            redirect_uri=url_for('oauth2.callback', _external=True),
            **kw)

        flow_key = _FLOW_KEY.format(csrf_token)
        session[flow_key] = pickle.dumps(flow)

        return flow
github googleapis / oauth2client / oauth2client / contrib / flask_util.py View on Github external
csrf_token = hashlib.sha256(os.urandom(1024)).hexdigest()

        session[_CSRF_KEY] = csrf_token

        state = json.dumps({
            'csrf_token': csrf_token,
            'return_url': return_url
        })

        kw = self.flow_kwargs.copy()
        kw.update(kwargs)

        extra_scopes = kw.pop('scopes', [])
        scopes = set(self.scopes).union(set(extra_scopes))

        flow = client.OAuth2WebServerFlow(
            client_id=self.client_id,
            client_secret=self.client_secret,
            scope=scopes,
            state=state,
            redirect_uri=url_for('oauth2.callback', _external=True),
            **kw)

        flow_key = _FLOW_KEY.format(csrf_token)
        session[flow_key] = pickle.dumps(flow)

        return flow
github google / loaner / loaner / deployments / lib / auth.py View on Github external
Raises:
      InvalidCredentials: when we are unable to get valid credentials for the
          user.
    """
    redirect = 'urn:ietf:wg:oauth:2.0:oob'
    creds_flags = argparse.ArgumentParser(
        parents=[tools.argparser]).parse_args(['--noauth_local_webserver'])
    if _run_local_web_server_for_auth():
      redirect = 'http://localhost:8080/oauth2callback'
      creds_flags = argparse.ArgumentParser(
          parents=[tools.argparser]).parse_args([
              '--auth_host_port=8080',
              '--auth_host_name=localhost',
          ])
    flow = oauth2_client.OAuth2WebServerFlow(
        client_id=self._config.client_id,
        client_secret=self._config.client_secret,
        scope=scopes,
        redirect_uri=redirect)
    try:
      old_credentials = tools.run_flow(
          flow, client_file.Storage(self._config.local_credentials_file_path),
          creds_flags)
    except oauth2_client.FlowExchangeError as err:
      raise InvalidCredentials(
          'Unable to get valid credentials: {}.'.format(err))

    if _remove_creds() and os.path.isfile(
        self._config.local_credentials_file_path):
      os.remove(self._config.local_credentials_file_path)
github jhpyle / docassemble / docassemble_base / docassemble / base / oauth.py View on Github external
def _get_flow(self):
        app_credentials = get_config('oauth', dict()).get(self.appname, dict())
        client_id = app_credentials.get('id', None)
        client_secret = app_credentials.get('secret', None)
        if client_id is None or client_secret is None:
            raise Exception('The application ' + self.appname + " is not configured in the Configuration")
        flow = oauth2client.client.OAuth2WebServerFlow(
            client_id=client_id,
            client_secret=client_secret,
            scope=self.scope,
            redirect_uri=re.sub(r'\?.*', '', interview_url()),
            auth_uri=self.auth_uri,
            token_uri=self.token_uri,
            access_type='offline',
            prompt='consent')
        return flow
    def get_credentials(self):