小编Ana*_*SWW的帖子

不打开浏览器Python打开授权网址

google_auth_oauthlib.flow在 Python 中使用授权 Google Oauth 2 帐户。我的代码如下所示:

from google_auth_oauthlib.flow import InstalledAppFlow

flow = InstalledAppFlow.from_client_secrets_file(
    "client_secret_929791903032.apps.googleusercontent.com.json",
    scopes=['profile', 'email'])

flow.run_local_server(open_browser=False)

session = flow.authorized_session()

profile_info = session.get(
    'https://www.googleapis.com/userinfo/v2/me').json()

print(profile_info)
Run Code Online (Sandbox Code Playgroud)

根据run_local_server()文档,我尝试设置,open_browser=False但 Google 为我提供了一个URL进行授权,它看起来像这样https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=929739191032-hpdm8djidqd8o5nqg2gk366efau34ea6usercontent.apps。 com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=profile+email&state=oHJmupijpVH2gJEPqTogVVHIEtbVXcr&access_type=offline

单击提供的链接后,我的浏览器会自动打开名为Sign in with Google的 UI ,然后我必须在浏览器上手动登录。

所以我的问题是如何在不打开浏览器的情况下打开授权网址?我希望我的代码无需手动操作即可自动授权。

python api google-oauth

7
推荐指数
1
解决办法
5179
查看次数

错误:1210:执行准备好的语句的参数数量不正确

我正在尝试使用 Python 将数据插入 MySQL。

出现这个错误的原因是什么?

编程错误:1210:执行准备好的语句的参数数量不正确

我的Python代码:

connection = mysql.connector.connect(host='localhost',
                         database='popsww2017',
                         user='root',
                         password='')
records_to_insert = [('---2q7vcZGU', 'Partner-provided', '35', '9s1Pvm0U8Gg8mRavZhVXdg', 'A663893851990558', '1066/2016/HDHT-Pops-Kha Ly', '1467', '0.100598')]
sql_insert_query = "INSERT INTO raw_music (`Video_ID`, `Content_Type`, `Video_Duration`, `Channel_ID`, `Asset_ID`, `Asset_Labels`, `Owned_Views`, `Partner_Revenue`) VALUES ( '%s', '%s' , '%s' , '%s', '%s' , '%s' , '%s' , '%s') "
cursor = connection.cursor(prepared=True)
result  = cursor.executemany(sql_insert_query,records_to_insert)
connection.commit()
Run Code Online (Sandbox Code Playgroud)

我的桌子:

Video_ID    varchar(50) utf8_unicode_ci     
Content_Type    varchar(100)    utf16_unicode_ci        
Video_Duration  int(11)         
Channel_ID  varchar(100)    utf8_unicode_ci     
Asset_ID    varchar(50) utf32_unicode_ci        
Asset_Labels    varchar(400)    utf32_unicode_ci        
Owned_Views int(20) …
Run Code Online (Sandbox Code Playgroud)

python mysql database-connection

5
推荐指数
1
解决办法
5272
查看次数

如何在 Python 中使用 Refresh Token Google API?

此功能用于从 Google 获取经过身份验证的服务。后

        API_SERVICE_NAME = 'youtubereporting'
        API_VERSION = 'v1'
        CLIENT_SECRETS_FILE = "client_secret_929791903032-hpdm8djidqd8o5nqg2gk66efau34ea6q.apps.googleusercontent.com.json"
        SCOPES = ['https://www.googleapis.com/auth/yt-analytics-monetary.readonly']
        def get_authenticated_service():
            flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
            credentials = flow.run_local_server()
            return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
Run Code Online (Sandbox Code Playgroud)

但我想使用 Refresh Token 自动进行身份验证,而无需打开浏览器。因此在上面的函数中添加一些代码来保存Refresh Token:

def get_authenticated_service():
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
    credentials = flow.run_local_server()
    with open('refresh.token', 'w+') as f:
        f.write(credentials._refresh_token)
    print('Refresh Token:', credentials._refresh_token)
    print('Saved Refresh Token to file: refresh.token')
    return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
Run Code Online (Sandbox Code Playgroud)

好的,那么在拥有刷新令牌后,如何使用它?我试图替换刷新令牌="ABCDEF456"但它不起作用

def get_authenticated_service():
    return build(API_SERVICE_NAME, API_VERSION, credentials="ABCDEF456")
Run Code Online (Sandbox Code Playgroud)

python oauth google-api youtube-api google-oauth

5
推荐指数
1
解决办法
8724
查看次数