首页 > 解决方案 > 关于谷歌智能家居令牌交换问题的行动

问题描述

我陷入了困境。

情况:

我们在移动应用 IOS/Android 和 Action On Google(智能家居)中使用 Firebase 身份验证。我们使用 Firebase 函数来处理请求。

问题

在 google 上操作的帐户链接是我的问题。但是我的身份验证 URL 可以完美运行,并且能够成功发回数据。但是当它调用令牌 url 时,我们会在这里遇到问题。

正如我所说,我们使用 Firebase 身份验证,我们不知道如何从 Firebase 获取访问令牌和刷新令牌以发送回 Google Home。

下面是我的令牌代码,我现在只是发送常量令牌,我不知道如何获得访问令牌,请帮助,我真的很感谢你。

令牌 URL Firebase 功能:

export const token = functions.https.onRequest(async (request, response) => {
  //admin.auth().getUser(response)
  const grantType = request.query.grant_type ?
    request.query.grant_type : request.body.grant_type;
  const secondsInDay = 86400; // 60 * 60 * 24
  const HTTP_STATUS_OK = 200;
  console.log(`Grant type ${grantType}`);


  const user = admin.auth().getUser(request.body.code);

  user.getIdToken(true).then(function(idToken) {

}).catch(function(error) {

});

  console.log(request.body.client_id);
  console.log('test');
  console.log(request.body.client_secret);


  let obj = {};
  if (grantType === 'authorization_code')  {

      obj = {
        token_type: 'bearer',
        access_token: 'jkljkhasdjhjhkdhasdsd',
        refresh_token: 'nkjcajkguiahdilawjd,bcjkbakdjhdlasd',
        expires_in: secondsInDay,
      };
  } else if (grantType === 'refresh_token') {


    obj = {
      token_type: 'bearer',
      access_token: 'njkhasiumnabkdukchaskjhkhad',
      expires_in: secondsInDay,
    };
  }
  response.status(HTTP_STATUS_OK)
    .json(obj);
});

标签: firebasefirebase-authenticationgoogle-cloud-functionsaccess-tokengoogle-smart-home

解决方案


我们使用 Firebase 身份验证,我们不知道如何从 Firebase 获取访问令牌和刷新令牌以发送回 Google Home。

Firebase 身份验证不会直接向用户公开功能并为用户验证 OAuth 令牌,它只是让您能够提供客户端用户登录和验证。您需要在 Firebase 身份验证之上实现自己的 OAuth 令牌管理。

我们有一个示例应用程序可以执行类似的操作,您可能会发现它很有帮助,还有一篇描述一些实现细节的配套博客文章。该示例使用 JWT 对 Firebase 身份验证用户数据进行编码,因此每个请求都可以确定如何将 Assistant 意图映射到 Firebaseuid值。


推荐阅读