我有一个私人谷歌云功能。因此,我想使用Authorization: Bearer token头对云函数进行身份验证。但我得到了401 Unauthorized作为回应。
我使用以下方法创建了私有云函数:
gcloud beta functions deploy MyFunction --runtime go111--trigger-http --memory 128 --region us-central1 --source gs://bucketname/code.zip我创建了一个服务帐户,并为它分配了访问云功能的权限:
gcloud beta functions 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现在,我下载了服务帐户json文件。
因此,现在为了访问云函数,我需要添加带有Bearer的授权头。为了生成承载令牌,我使用以下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();我在授权头中使用此令牌。
URL- us-central1-project.cloudfunctions.net/MyFunction 头头- 授权:无记名ya29.c.YpN3bEGV-H4WyTeLQn9kxxFq1Js7mcMtoESW1C-5HstmEgdaXR_gY_stxnlpJna8IL25VvnpwTg5tFL5OorcfBT2_Qtld7FViTbzHas4AiUUEme7mffXRtZOn29
我用邮递员发送请求。对于这个请求,我得到了输出:
<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>有什么不对的?令牌生成逻辑有什么不同吗?
发布于 2019-07-20 20:49:47
有三种类型的OAuth令牌。刷新令牌,访问令牌,标识令牌。
您正在尝试使用OAuth访问令牌而不是OAuth身份令牌。
对于授权,Google函数在OAuth header中使用authorization: bearer标识令牌。
在您的问题中,您没有显示设置了对云函数的IAM成员访问。这意味着,无论你使用什么身份标记,都将被拒绝。
发布于 2020-11-06 20:01:00
这是我的解决方案:
Private Shared Async Function GetAccessToken(pCredentialJson As String, pFunctionPath As String) As Task(Of String)
Try
Dim scopes = New String() {"https://www.googleapis.com/auth/indexing"}
Dim credential As ServiceAccountCredential = TryCast(GoogleCredential.FromJson(pCredentialJson).CreateScoped(scopes).UnderlyingCredential, ServiceAccountCredential)
Dim oidcToken = Await credential.GetOidcTokenAsync(OidcTokenOptions.FromTargetAudience(pFunctionPath))
Return Await oidcToken.GetAccessTokenAsync()
Catch ex As Exception
Throw
End Try
End Function将返回的值放在头授权Bearer ...中。
https://stackoverflow.com/questions/57125081
复制相似问题