当前位置: 首页>>代码示例>>Python>>正文


Python InstalledAppFlow.from_client_secrets_file方法代码示例

本文整理汇总了Python中google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file方法的典型用法代码示例。如果您正苦于以下问题:Python InstalledAppFlow.from_client_secrets_file方法的具体用法?Python InstalledAppFlow.from_client_secrets_file怎么用?Python InstalledAppFlow.from_client_secrets_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在google_auth_oauthlib.flow.InstalledAppFlow的用法示例。


在下文中一共展示了InstalledAppFlow.from_client_secrets_file方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: getCreds

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def getCreds():
  # The file token.pickle stores the user's access and refresh tokens, and is
  # created automatically when the authorization flow completes for the first
  # time.
  creds = None
  SCOPES = 'https://www.googleapis.com/auth/drive'

  if os.path.exists('token.pickle'):
      with open('token.pickle', 'rb') as token:
          creds = pickle.load(token)
  # If there are no (valid) credentials available, let the user log in.
  if not creds or not creds.valid:
      if creds and creds.expired and creds.refresh_token:
          creds.refresh(Request())
      else:
          flow = InstalledAppFlow.from_client_secrets_file(
              'credentials.json', SCOPES)
          creds = flow.run_local_server(port=0)
      # Save the credentials for the next run
      with open('token.pickle', 'wb') as token:
          pickle.dump(creds, token)

  return creds 
开发者ID:zume2020,项目名称:Telegram-bot-Google-Drive,代码行数:25,代码来源:bot.py

示例2: from_client_secrets_file

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def from_client_secrets_file(cls, client_secrets_file, scopes, **kwargs):
        """Creates a :class:`Flow` instance from a Google client secrets file.

        Args:
            client_secrets_file (str): The path to the client secrets .json
                file.
            scopes (Sequence[str]): The list of scopes to request during the
                flow.
            kwargs: Any additional parameters passed to
                :class:`requests_oauthlib.OAuth2Session`

        Returns:
            Flow: The constructed Flow instance.
        """
        with open(client_secrets_file, "r") as json_file:
            client_config = json.load(json_file)

        return cls.from_client_config(client_config, scopes=scopes, **kwargs) 
开发者ID:googleapis,项目名称:google-auth-library-python-oauthlib,代码行数:20,代码来源:flow.py

示例3: __init__

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def __init__(self, address):
        """
        Initialise a GMail notifier object. Prompt the user to authenticate
        and provide mailing permissions if required.
        """
        self.address = address
        self.creds = None
        # if there's an access token from previous authentication, load it
        if os.path.exists(ACCESS_TOKEN_PATH):
            with open(ACCESS_TOKEN_PATH, 'rb') as tokenfile:
                self.creds = pickle.load(tokenfile)

        # if the credentials are invalid or non-existent, prompt to authenticate
        if not self.creds or not self.creds.valid:
            if self.creds and self.creds.expired and self.creds.refresh_token:
                self.creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file(
                    CLIENT_ID_PATH, SCOPES)
                self.creds = flow.run_local_server()
            # save the credentials for the next run
            with open(ACCESS_TOKEN_PATH, 'wb') as tokenfile:
                pickle.dump(self.creds, tokenfile)

        self.service = build('gmail', 'v1', credentials=self.creds) 
开发者ID:matomatical,项目名称:UoM-WAM-Spam,代码行数:27,代码来源:by_email_oauth.py

示例4: get_creds

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def get_creds(credentials,token,scopes=['https://www.googleapis.com/auth/drive']):
    creds = None

    if path.exists(token):
        with open(token,'r') as t:
            creds = json_to_cred(t)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(credentials,scopes)
            creds = flow.run_local_server(port=0)
        with open(token,'w') as t:
            json.dump(cred_to_json(creds),t,indent=2)

    return creds 
开发者ID:Spazzlo,项目名称:folderclone,代码行数:18,代码来源:_helpers.py

示例5: authorize

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def authorize(self):
        # Get credentials
        credentials = None
        if not USE_SERVICE_ACCOUNTS:
            if os.path.exists(self.__G_DRIVE_TOKEN_FILE):
                with open(self.__G_DRIVE_TOKEN_FILE, 'rb') as f:
                    credentials = pickle.load(f)
            if credentials is None or not credentials.valid:
                if credentials and credentials.expired and credentials.refresh_token:
                    credentials.refresh(Request())
                else:
                    flow = InstalledAppFlow.from_client_secrets_file(
                        'credentials.json', self.__OAUTH_SCOPE)
                    LOGGER.info(flow)
                    credentials = flow.run_console(port=0)

                # Save the credentials for the next run
                with open(self.__G_DRIVE_TOKEN_FILE, 'wb') as token:
                    pickle.dump(credentials, token)
        else:
            LOGGER.info(f"Authorizing with {SERVICE_ACCOUNT_INDEX}.json service account")
            credentials = service_account.Credentials.from_service_account_file(
                f'accounts/{SERVICE_ACCOUNT_INDEX}.json',
                scopes=self.__OAUTH_SCOPE)
        return build('drive', 'v3', credentials=credentials, cache_discovery=False) 
开发者ID:lzzy12,项目名称:python-aria-mirror-bot,代码行数:27,代码来源:gdriveTools.py

示例6: set_google_credentials

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def set_google_credentials(authorized_user_file,
                           key_file_location,
                           scope):

    try:
        credentials = Credentials.from_authorized_user_file(authorized_user_file, scopes=[scope])
    except FileNotFoundError:
        flow = InstalledAppFlow.from_client_secrets_file(key_file_location, [scope])
        credentials = flow.run_console()
        os.makedirs(os.path.expanduser('~/.cache/'), exist_ok=True)
        with open(authorized_user_file, 'w') as file:
            json.dump({
                'client_id': credentials._client_id,
                'client_secret': credentials._client_secret,
                'refresh_token': credentials._refresh_token
            }, file)
        os.chmod(authorized_user_file, 0o600)
    return credentials 
开发者ID:Wikia,项目名称:sroka,代码行数:20,代码来源:config.py

示例7: init_email_sending

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def init_email_sending():
    SCOPES = ['https://www.googleapis.com/auth/gmail.send']

    creds = None

    try:
        if os.path.exists('token.pickle'):
            with open('token.pickle', 'rb') as token:
                creds = pickle.load(token)

        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
                creds = flow.run_console()
            with open('token.pickle', 'wb') as token:
                pickle.dump(creds, token)

        service = build('gmail', 'v1', credentials=creds)
        return True
    except Exception as e:
        print(e)
        return False 
开发者ID:TruSat,项目名称:trusat-backend,代码行数:26,代码来源:google_email.py

示例8: get_authenticated_service

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
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) 
开发者ID:shobeir,项目名称:GraphiPy,代码行数:6,代码来源:youtube_api.py

示例9: __init__

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def __init__(self, oauth_client_secrets_path):
    flow = InstalledAppFlow.from_client_secrets_file(
        oauth_client_secrets_path,
        scopes=['https://www.googleapis.com/auth/siteverification'])
    credentials = flow.run_console()

    http = google_auth_httplib2.AuthorizedHttp(
        credentials, http=httplib2.Http())

    self.api = discovery.build('siteVerification', 'v1', http=http) 
开发者ID:google,项目名称:clusterfuzz,代码行数:12,代码来源:create_config.py

示例10: set_session

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def set_session(self):
        # Try to load credentials from an auth file.
        # If it doesn't exist or is not valid then catch the 
        #  exception and reauthenticate.
        try:
            creds = Credentials.from_authorized_user_file(self.auth_file, self.scopes)
        except:
            try:
                flow = InstalledAppFlow.from_client_secrets_file(self.secrets_file, self.scopes)
                creds = flow.run_local_server()
                cred_dict = {
                    'token': creds.token,
                    'refresh_token': creds.refresh_token,
                    'id_token': creds.id_token,
                    'scopes': creds.scopes,
                    'token_uri': creds.token_uri,
                    'client_id': creds.client_id,
                    'client_secret': creds.client_secret
                }

                # Store the returned authentication tokens to the auth_file.
                with open(self.auth_file, 'w') as f:
                    f.write(json.dumps(cred_dict))
            except:
                return

        self.session = AuthorizedSession(creds)
        self.session.headers["Content-type"] = "application/octet-stream"
        self.session.headers["X-Goog-Upload-Protocol"] = "raw" 
开发者ID:jmathai,项目名称:elodie,代码行数:31,代码来源:googlephotos.py

示例11: matrix_start

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def matrix_start(self, bot):
        super().matrix_start(bot)
        self.bot = bot
        creds = None

        if not os.path.exists(self.credentials_file) or os.path.getsize(self.credentials_file) == 0:
            return  # No-op if not set up

        if os.path.exists('token.pickle'):
            with open('token.pickle', 'rb') as token:
                creds = pickle.load(token)
                self.logger.info('Loaded existing pickle file')
        # If there are no (valid) credentials available, let the user log in.
        if not creds or not creds.valid:
            self.logger.warn('No credentials or credentials not valid!')
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file(self.credentials_file, self.SCOPES)
                # urn:ietf:wg:oauth:2.0:oob
                creds = flow.run_local_server(port=0)
            # Save the credentials for the next run
            with open('token.pickle', 'wb') as token:
                pickle.dump(creds, token)
                self.logger.info('Pickle saved')

        self.service = build('calendar', 'v3', credentials=creds)

        try:
            calendar_list = self.service.calendarList().list().execute()['items']
            self.logger.info(f'Google calendar set up successfully with access to {len(calendar_list)} calendars:\n')
            for calendar in calendar_list:
                self.logger.info(f"{calendar['summary']} - + {calendar['id']}")
        except Exception:
            self.logger.error('Getting calendar list failed!') 
开发者ID:vranki,项目名称:hemppa,代码行数:37,代码来源:googlecal.py

示例12: main

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def main(client_secrets_path, scopes):
    flow = InstalledAppFlow.from_client_secrets_file(
        client_secrets_path, scopes=scopes)

    flow.run_console()

    print('Access token: %s' % flow.credentials.token)
    print('Refresh token: %s' % flow.credentials.refresh_token) 
开发者ID:googleads,项目名称:google-ads-python,代码行数:10,代码来源:authenticate_in_standalone_application.py

示例13: main

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def main(client_secrets_path, scopes):
    flow = InstalledAppFlow.from_client_secrets_file(
        client_secrets_path, scopes=scopes)

    flow.run_local_server()

    print('Access token: %s' % flow.credentials.token)
    print('Refresh token: %s' % flow.credentials.refresh_token) 
开发者ID:googleads,项目名称:google-ads-python,代码行数:10,代码来源:authenticate_in_web_application.py

示例14: authenticate

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def authenticate():
    print('Authenticating')
    # If modifying these scopes, delete the file token.pickle.
    SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)

    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            print('Refreshing credentials')
            creds.refresh(Request())
        else:
            print('Need to allow access')
            flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('calendar', 'v3', credentials=creds)
    return service 
开发者ID:gillescastel,项目名称:university-setup,代码行数:29,代码来源:countdown.py

示例15: get_authenticated_service

# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
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) 
开发者ID:ItsCEED,项目名称:Youtube-Comment-Bot,代码行数:6,代码来源:yt.py


注:本文中的google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。