use*_*835 5 google-cloud-platform google-cloud-functions
我有一个私有的谷歌云功能。所以我想使用Authorization: Bearer tokenHeader对 Cloud Function 进行身份验证。但我得到401 Unauthorized的回应。
我使用以下方法创建了私有云函数:
gcloud beta 函数部署 MyFunction --runtime go111 --trigger-http --memory 128 --region us-central1 --source gs://bucketname/code.zip
我创建了一个服务帐户并为其分配了访问云功能的权限:
gcloud beta 函数 add-iam-policy-binding MyFunction --member=serviceAccount:cf-access@my-project.iam.gserviceaccount.com --role=roles/cloudfunctions.admin
输出:
bindings:
- members:
- serviceAccount:cf-access@my-project.iam.gserviceaccount.com
role: roles/cloudfunctions.admin
etag: BwWOGyVdpDg=
version: 1
Run Code Online (Sandbox Code Playgroud)
现在,我下载了服务帐户 json 文件。
所以现在为了访问 Cloud Function,我需要添加带有 Bearer 令牌的 Authorization Header。为了生成不记名令牌,我使用以下 java 代码:
String fileName = "path/service-account.json";
FileInputStream keyFileInputStream = new FileInputStream(fileName);
GoogleCredential credential = GoogleCredential.fromStream(keyFileInputStream).createScoped(Collections.singleton(ContainerScopes.CLOUD_PLATFORM));
credential.refreshToken();
String token = credential.getAccessToken();
Run Code Online (Sandbox Code Playgroud)
我在授权标头中使用此令牌。
网址-
us-central1-[项目].cloudfunctions.net/MyFunction
标题-
授权:承载 ya29.c.YpN3bEGV-H4WyTeLQn9kxxFq1Js7mcMtoESW1C-5HstmEgdaXR_gY_stxnlpJna8IL25VvnpwTg5tFL5OorcfBT2_QtldHa7FViTAffiTmXUzR
我使用邮递员发送请求。对于这个请求,我得到输出:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>401 Unauthorized</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Unauthorized</h1>
<h2>Your client does not have permission to the requested URL
<code>/MyFunction</code>.
</h2>
<h2></h2>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
可能有什么问题?令牌生成逻辑有什么不同吗?
OAuth 令牌分为三种类型。刷新令牌、访问令牌、身份令牌。
您正在尝试使用 OAuth 访问令牌而不是 OAuth 身份令牌。
对于授权,Google Cloud Functions 在 HTTP 标头中使用 OAuth 身份令牌authorization: bearer。
在您的问题中,您没有显示设置 IAM 成员对云功能的访问权限。这意味着无论您使用什么身份令牌,都将被拒绝。
| 归档时间: |
|
| 查看次数: |
1733 次 |
| 最近记录: |