Python 如何在没有web浏览器的情况下使用google drive API

Python 如何在没有web浏览器的情况下使用google drive API,python,oauth-2.0,google-api,google-drive-api,google-authentication,Python,Oauth 2.0,Google Api,Google Drive Api,Google Authentication,我正在编写一个python脚本,试图将所有需要的配置文件从Linux虚拟机备份到Google Drive Cloud。我想自动执行,而无需在每次脚本启动时从浏览器中输入验证代码。你能告诉我怎么做吗 #!/usr/bin/python import httplib2 import pprint import credentials as cred from apiclient.discovery import build from apiclient.http import MediaFileU

我正在编写一个python脚本,试图将所有需要的配置文件从Linux虚拟机备份到Google Drive Cloud。我想自动执行,而无需在每次脚本启动时从浏览器中输入验证代码。你能告诉我怎么做吗

#!/usr/bin/python

import httplib2
import pprint
import credentials as cred
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow, FlowExchangeError

# Path to the file to upload
FILENAME = 'hello.py'

# Run through the OAuth flow and retrieve credentials from credentials.py


def api_upload(FILENAME):

    flow = OAuth2WebServerFlow(cred.credentials['CLIENT_ID'], cred.credentials['CLIENT_SECRET'],
                               cred.credentials['OAUTH_SCOPE'],
                               redirect_uri=cred.credentials['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 = ''
    try:
        credentials = flow.step2_exchange(code)
    except FlowExchangeError:
        print "Your verification code is incorrect or something else is broken."
        exit(1)

    # Create an httplib2.Http object and authorize it with our credentials
    http = httplib2.Http()
    http = credentials.authorize(http)

    drive_service = build('drive', 'v2', http=http)

    # Insert a file
    try:
        media_body = MediaFileUpload(FILENAME, mimetype='text/plain',     resumable=True)
        body = {
            'title': "" + FILENAME,
            'description': 'A test document',
            'mimeType': 'text/plain'
        }
        upload_file = drive_service.files().insert(body=body, media_body=media_body).execute()
        pprint.pprint(upload_file)
    except IOError:
        print "No such file"
        exit(1)

# Function usage:
api_upload(FILENAME)
这是我的示例函数

另一个文件存储请求的凭据:

credentials = {"CLIENT_ID": 'blablabla',
               "CLIENT_SECRET": 'blablabla',
               "OAUTH_SCOPE": 'https://www.googleapis.com/auth/drive',
               "REDIRECT_URI": 'urn:ietf:wg:oauth:2.0:oob'}

基本上,您希望将此脚本分为两个单独的脚本,一个获取代码并将其交换为访问令牌,另一个使用访问令牌访问您的Google驱动器。我将上述代码分为两个部分,并做了以下更改

在第一部分中,您可以像上面那样继续,但在获取代码的初始查询中,将access\u type设置为“offline”,将approval\u prompt设置为“force”(请参阅)。如果您这样做,那么当您交换代码时,返回的令牌将不仅包含一个access\u令牌,还包含一个refresh\u令牌,该令牌可用于无限期地刷新access令牌。因此,第一个脚本应该将这些令牌保存到一个文件中并退出。如果你能做到这一点,那么你只需要运行这段代码(手动干预)一次


然后,您的第二个脚本可以在需要运行时从文件中加载这些保存的令牌,并访问您的文档,而无需您进行干预

您能告诉我们您面临的问题是什么吗?问题是每次授权url=flow。步骤1\u get\u authorize\u url()打印“转到浏览器中的以下链接:”+authorize\u url您应该打开浏览器,按continue并将验证代码粘贴到控制台。如何在不打开浏览器的情况下执行此操作?请使用正确格式的问题编辑问题。这将在其他s.o.问题中介绍。另一个也没有答案,两个都是DUP,但我没有时间搜索它们。