使用GoogleCredentials获取用于Firestore访问的承载令牌

时间:2019-05-29 20:35:03

标签: .net authentication oauth-2.0 google-cloud-firestore

我想通过rest api调用将数据存储在Firestore上。我已经解析了googlecredentials可以下载的服务帐户json文件。我还将令牌的范围限定为

https://www.googleapis.com/auth/datastore

当我将令牌传递到授权请求标头并将请求发送到

https://firestore.googleapis.com/v1/projects/...

我仅收到401-请求具有无效的身份验证凭据。预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据...

我尝试从

获取ID令牌
https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=abcde 

并传递它而不是生成的自定义标记。这也不起作用。

获取ID令牌(GetFirebaseToken从服务帐户json返回自定义令牌)。

var token = string.Empty;

using(var http = new HttpClient())
{
    var res = await 
 http.PostAsJsonAsync("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=abcde", new { token = await 
   GetFirebaseToken("api", new Dictionary<string, object>()) });

    var content = await res.Content.ReadAsStringAsync();
    var obj = JObject.Parse(content);
    token = obj["idToken"].Value<string>();
}

尝试修补文档

using(var http = new HttpClient())
{
   var content = new 
   StringContent(JsonConvert.SerializeObject(appointment));
   http.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");

var res = await
http.PatchAsync("https://firestore.googleapis.com/v1/projects/myproject/databases/(default)/documents/appointments/" + appointment.Id,
                          content);

    Console.WriteLine(await res.Content.ReadAsStringAsync());
}

如何从其余端点获取有效令牌以访问Firestore API?

0 个答案:

没有答案