Pygsheets:无法使用谷歌服务帐户将超过200个ish文件上传到Drive.获取403错误



我使用pygsheets来处理Google电子表格和Drive。我需要将超过10000个文件上传到公司Drive,但每次使用服务帐户时,我都会在上传200个ish文件后出错。错误如下:

{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}

我检查了我的云控制台。在任何给定的时间内,我都不会过度使用对请求数量的限制。每次请求间隔10秒。

在下面附上相关代码:

def upload_cluster(sheets, cluster_array):
max_global = 0
max_local = 0
max_traffic = 0
sum_global = 0
sum_local = 0
sum_traffic = 0
keywords = len(cluster_array)
cluster_name = ''
for elem in cluster_array:
if elem.get('Global volume') >= max_global:
cluster_name = elem.get('Keyword')
max_global = elem.get('Global volume')
if elem.get('Volume') > max_local:
max_local = elem.get('Volume')
if elem.get('Traffic potential') > max_traffic:
max_traffic = elem.get('Traffic potential')
sum_global += elem.get('Global volume')
sum_local += elem.get('Volume')
sum_traffic += elem.get('Traffic potential')
book = sheets.create(title=re.sub('"', '', cluster_name), folder='FOLDER_ID')
link = f'HYPERLINK("https://docs.google.com/spreadsheets/d/{book.id}","{cluster_name}")'
dataframe = pandas.DataFrame(cluster_array)
out_sheet = book.worksheet(property='index', value=0)
out_sheet.set_dataframe(df=dataframe, start='A1', extend=True, copy_head=True, copy_index=False)
cluster_summary = {
'Cluster': link,
'Volume': sum_local,
'Global volume': sum_global,
'Traffic potential': sum_traffic,
'Max volume': max_local,
'Max global volume': max_global,
'Max traffic potential': max_traffic,
'Queries': keywords
}
return cluster_summary
def main():
gsheets = pygsheets.authorize(service_account_file='service-account.json')
for i in range(len(output_keywords) - 1):
if not output_keywords[i].get('Clustered'):
cluster = []
cluster.append(output_keywords[i])
cluster_max = get_vol(output_keywords[i].get('Global volume'))
cluster_urls = output_keywords[i].get('URLs')
output_keywords[i]['Clustered'] = True
print(f'Added to cluster: {cluster[-1]}')
for j in range(len(output_keywords)):
if not output_keywords[j].get('Clustered'):
if len(set(cluster_urls) & set(output_keywords[j].get('URLs'))) >= 4:
cluster.append(output_keywords[j])
output_keywords[j]['Clustered'] = True
print(f'Added to cluster: {cluster[-1]}')
print('Uploading cluster...')
clusters.append(upload_cluster(gsheets, cluster))
sleep(5)
print(f'Uploaded: {clusters[-1]}')
cluster = []

我也尝试过通过客户端秘密进行授权,看起来效果不错,但不幸的是,我在文件夹中看不到上传的文件。

服务帐户可以访问驱动器文件夹。

经过更多的测试,我注意到脚本阻塞了一个特定文件,因为该文件的名称中有'。在使用RegEx从文件名中删除'之后,它没有阻塞。奇怪,但还好。有人想过为什么会发生这种事吗?我以为谷歌硬盘没有禁止命名文件的字符。

最新更新