答案取决于您的环境以及您如何创建/获取凭据。
什么是Google Cloud凭据?
Google Cloud凭证是OAuth 2.0令牌。此令牌至少具有
Access Token和
RefreshToken,并且可选地包含
Client ID Token,和支持参数,例如
expiration,
Service AccountEmail或
Client Email等。
Google Cloud API中的重要项目是
AccessToken。该令牌授权访问云。该令牌可用于诸如
curl,诸如的软件等程序中
python,并且不需要SDK。的
AccessToken是在HTTP中使用
Authorization报头。
什么是访问令牌?
访问令牌是Google生成的不透明值,它是从Signed
JWT(更正确地称为JWS)派生而来的。JWT由标头和声明(有效负载)Json结构组成。这两个Json结构是使用服务帐户的私钥签名的。这些值经过base64编码和连接以创建访问密钥。
访问令牌的格式为:
base64(header) + '.' + base64(payload) + '.' + base64(signature)。
这是一个JWT示例:
标头:
{ "alg": "RS256", "typ": "JWT", "kid": "42ba1e234ac91ffca687a5b5b3d0ca2d7ce0fc0a"}有效负载:
{ "iss": "myservice@myproject.iam.gserviceaccount.com", "iat": 1493833746, "aud": "myservice.appspot.com", "exp": 1493837346, "sub": "myservice@myproject.iam.gserviceaccount.com"}使用访问令牌:
启动虚拟机实例的示例。替换PROJECT_ID,ZONE和INSTANCE_NAME。此示例适用于Windows。
curl -v -X GET -H "Authorization: Bearer <access_token_here>" ^https://www.googleapis.com/compute/v1/projects/%PROJECT_ID%/zones/%ZONE%/instances/%INSTANCE_NAME%/start
Compute Engine服务帐户:
在这种情况下,达斯汀的答案是正确的,但是为了完整起见,我将提供一些附加信息。
这些凭据由GCP自动为您创建,并从VM实例元数据中获取。权限由
Cloud API access scopesGoogle控制台控制。
但是,这些凭据有一些限制。要修改凭据,必须首先停止VM实例。此外,不支持所有权限(角色)。
from google.auth import compute_enginecred = compute_engine.Credentials()
服务帐户凭据:
在您了解凭证的所有类型及其用例之前,这些凭证将用于除
gcloud和以外的所有内容
gsutil。了解这些凭据将使编写程序时使用Google
Cloud变得更加简单。从Google Service Account
Json文件获取凭证很容易。唯一需要注意的是凭证过期(通常为60分钟),并且需要刷新或重新创建。
gcloud auth print-access-token不推荐。服务帐户凭据是Google推荐的方法。
这些凭证由控制台,gcloud或通过程序/ API创建。权限由IAM分配给贷方,并在Compute Engine,App
Engine,Firestore,Kubernetes等以及Google Cloud之外的其他环境中起作用。这些凭证从Google
Cloud下载并存储在Json文件中。注意该
scopes参数。这定义了授予结果凭证对象的权限。
SCOPES = ['https://www.googleapis.com/auth/sqlservice.admin']SERVICE_ACCOUNT_FILE = 'service-account-credentials.json'from google.oauth2 import service_accountcred = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=SCOPES)
Google OAuth 2.0凭证:
这些凭据来自完整的OAuth
2.0流。这些凭据是在启动浏览器以访问Google帐户以授权访问时生成的。该过程要复杂得多,需要大量代码来实现,并且需要内置的Web服务器来进行授权回调。
此方法提供了其他功能,例如能够在浏览器中运行所有内容,例如,您可以创建云存储文件浏览器,但是请务必了解安全隐患。此方法是用于支持Google登录等的技术。我喜欢在允许在网站等上发布之前使用此方法对用户进行身份验证。使用正确授权的OAuth
2.0身份和范围的可能性是无限的。
使用的示例代码google_auth_oauthlib
:
from google_auth_oauthlib.flow import InstalledAppFlowflow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', scopes=scope)cred = flow.run_local_server( host='localhost', port=8088, authorization_prompt_message='Please visit this URL: {url}', success_message='The auth flow is complete; you may close this window.', open_browser=True)使用该requests_oauthlib
库的示例代码:
from requests_oauthlib import OAuth2Sessiongcp = OAuth2Session( app.config['gcp_client_id'], scope=scope, redirect_uri=redirect_uri)# print('Requesting authorization url:', authorization_base_url)authorization_url, state = gcp.authorization_url( authorization_base_url, access_type="offline", prompt="consent", include_granted_scopes='true')session['oauth_state'] = statereturn redirect(authorization_url)# Next section of pre after the browser approves the requesttoken = gcp.fetch_token( token_url, client_secret=app.config['gcp_client_secret'], authorization_response=request.url)


