通过Google Drive API从Google Sheet导出链接

时间:2018-07-12 15:45:38

标签: python google-sheets

是否可以通过python的Google驱动器api导出到已发布工作表的链接(例如下图)?linkpublishedsheet

google drive api tutorial.m中的脚本代码

@app.route('/test')
def test_api_request():
    if 'credentials' not in session:
        return redirect('authorize')
    credentials = google.oauth2.credentials.Credentials(**session['credentials'])
    drive = googleapiclient.discovery.build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
    files = drive.files().list(q="mimeType='application/vnd.google-apps.spreadsheet'").execute().get('items', [])
    for f in files:
        print(f)
        print(" ")  
    session['credentials'] = credentials_to_dict(credentials)
    res = requests.get('https://docs.google.com/spreadsheets/d/11W_MaQm6UCMhufuKQCcXYw10ggkTyzz_mOsvGOgOXN0/pub?gid=0&single=true&output=csv')
    return(res.content)

@app.route('/authorize')
def authorize():
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(CLIENT_SECRETS_FILE, scopes=SCOPES)
    flow.redirect_uri = url_for('oauth2callback', _external=True)
    authorization_url, state = flow.authorization_url(access_type='offline', include_granted_scopes='true')
    session['state'] = state
    return redirect(authorization_url)

@app.route('/oauth2callback')
def oauth2callback():
    state = session['state']
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
    flow.redirect_uri = url_for('oauth2callback', _external=True)
    authorization_response = request.url
    flow.fetch_token(authorization_response=authorization_response)
    credentials = flow.credentials
    session['credentials'] = credentials_to_dict(credentials)
    return redirect(url_for('test_api_request'))

1 个答案:

答案 0 :(得分:0)

从您问题中的URL,它可能是网络发布的电子表格的URL。不幸的是,如果我的理解没有错,则Drive API和其他API无法直接检索该URL。但是可以使用电子表格的文件ID创建相同的链接。如果您可以检索文件ID,请尝试以下URL?您可以选择其中之一。

https://docs.google.com/spreadsheet/pub?key=### fileId ###

https://docs.google.com/spreadsheets/d/### fileId ###/pubhtml

如果我误解了你的问题,对不起。