在Python中创建一个airtable OAuth令牌

问题描述 投票:0回答:1
我有来自 Google 的代码,它成功使用

google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file

 API 打开浏览器并创建 Google API Oauth2 令牌,该令牌存储在文件中并在 API 调用中使用:

from google_auth_oauthlib.flow import InstalledAppFlow def get_credentials(): creds = None # The file token.json 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_FILENAME): creds = Credentials.from_authorized_user_file(TOKEN_FILENAME, SCOPES) # 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_FILENAME, SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open(TOKEN_FILENAME, 'w') as token: token.write(creds.to_json()) return creds

CREDENTIALS_FILENAME

 是从 Google OAuth 控制台下载的 JSON 文件。

Airtable 支持 OAuth,但它不下载易于使用的凭据文件。但我仍然能够获得所有关键参数。我将这些放入 JSON 文件并尝试了 Google 的代码,但没有成功。

以下是我尝试过的关键参数:

{"installed": {"client_id":"****", "auth_uri":"https://airtable.com/oauth2/v1/authorize", "token_uri":"https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs", "client_secret":"*****", "redirect_uris":["http://localhost"]}}
Airtable 给我这个错误:

嗯,我的客户 ID 是正确的。 Google 使用的是

http://localhost

。 Google 
InstalledAppFlow.from_client_secrets_file
 在随机端口上运行本地网络服务器,以从重定向的网络浏览器获取响应。不幸的是,
from_client_secrets_file
不允许我指定端口。

我已经尝试过使用 Airtable 的其他 OAuth2 库,但也无法让它们工作。我有很多代码。大多数情况下,我需要从控制台复制 URL,将其粘贴到浏览器中,然后获取重定向 URL(这会产生错误)并将其粘贴到控制台中。

这段代码已经很远了:

from oauthlib.oauth2 import WebApplicationClient from requests_oauthlib import OAuth2Session import secrets import base64 import hashlib def generate_pkce_pair(): """ Generate a code_verifier and code_challenge for PKCE. """ code_verifier = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b"=").decode('utf-8') code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode('utf-8')).digest()).rstrip(b"=").decode('utf-8') return code_verifier, code_challenge def main(): client_id = '****' redirect_uri = 'https://localhost:8080/' scope = ['schema.bases:read'] # Generate PKCE code verifier and challenge code_verifier, code_challenge = generate_pkce_pair() # OAuth2 client client = WebApplicationClient(client_id) oauth = OAuth2Session(client=client, redirect_uri=redirect_uri, scope=scope) # Authorization URL authorization_url, state = oauth.authorization_url( url='https://airtable.com/oauth2/v1/authorize', code_challenge_method="S256", code_challenge=code_challenge ) print("Please go to this URL and authorize:", authorization_url) # Get the authorization code from the callback URL redirect_response = input('Paste the full redirect URL here: ') # Fetch the access token token_url = "https://api.airtable.com/oauth/token" token = oauth.fetch_token( token_url=token_url, authorization_response=redirect_response, client_secret=None, code_verifier=code_verifier) print("Access token:", token) if __name__ == '__main__': main()
给我这个:

重定向到无法读取的

localhost:8080

 URL。没关系,我直接将重定向 URL 粘贴到控制台中,但随后出现此错误:

oauthlib.oauth2.rfc6749.errors.CustomOAuth2Error: ({'type': 'INVALID_API_VERSION'})
而且我不知道如何指定 API 版本。

所以我需要:

1 - 为airtable指定一个与Google库配合使用的有效redirect_uri,或者 2 - 指定

OAuth2Session

 调用的 API 版本。

oauth airtable
1个回答
0
投票
所以我需要:

1 - 指定与 Google 库配合使用的airtable 的有效重定向_uri,或 2 - 指定 OAuth2Session 调用的 API 版本。

根据文档,下

访问令牌将在 60 分钟后过期。您将需要使用从您的请求返回的refresh_token到

/oauth2/v1/token

(来自新授权和刷新令牌时,如下所述)在过期后请求新的访问令牌。

这是通过使用以下标头和请求正文向

/oauth2/v1/token

 发出新的 POST 请求来完成的。

所以,改变你的

token_url = "https://api.airtable.com/oauth/token"

token_url = "https://api.airtable.com/oauth2/v1/token"
    
© www.soinside.com 2019 - 2024. All rights reserved.