如何从用于智能家居的谷歌操作中使用的 OAuth 获取访问令牌 [英] How to fetch access token from OAuth used in google action for smart home

查看:12
本文介绍了如何从用于智能家居的谷歌操作中使用的 OAuth 获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的智能家居操作,我使用了假身份验证,如 codelab-smartwasher 应用程序中所示.(用于测试目的).该应用程序运行良好.我已经构建了自己的代码来处理我的设备(交换机).现在,当我实现使用我自己的自定义 OAuth 服务器的 OAuth 时.我无法弄清楚如何在我的代码中实现它.当我测试时,OAuth 正在根据需要工作.但我需要帮助将其与 google action 集成.我在获取访问令牌时遇到问题.代码如下:

For my smart home action I used fake auth as shown in codelab- smartwasher application. (For testing purpose ). The app is working fine. I have build my own code to work with my devices(Switches). Now When I am implementing OAuth which uses my own custom OAuth server. I am not able to figure out how to implement it in my code. The OAuth is working as needed when I tested. But I want help in integrating it with google action. I am facing problem fetching access token. The code is as follows:

exports.fakeauth = functions.https.onRequest((request, response) => {
      const responseurl = util.format('%s?code=%s&state=%s',
        decodeURIComponent(request.query.redirect_uri), request.query.code,
        request.query.state);
      console.log('*********'+responseurl);
      return response.redirect(responseurl);
    });

    exports.faketoken = functions.https.onRequest((request, 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}`);

      let obj;
      if (grantType === 'authorization_code') {
        obj = {
          token_type: 'bearer',
          access_token: '123access',
          refresh_token: '123refresh',
          expires_in: secondsInDay,
        };
      } else if (grantType === 'refresh_token') {
        obj = {
          token_type: 'bearer',
          access_token: '123access',
          expires_in: secondsInDay,
        };
      }
      response.status(HTTP_STATUS_OK)
        .json(obj);
        console.log('********** TOKEN **********',response);
    });

以上代码使用假身份验证执行.为什么在我实现自定义 OAuth 时没有执行?我需要对 firebase 中的 clienID 和 secret 做任何更改吗?如何获取 OAuth 返回的访问令牌?

The above code executes with fake auth. Why is is not executing when I am implmenting custom OAuth? Do I need to do any changes for clienID and secret in firebase? How to fetch access token returned by OAuth?

请帮忙.我是 node.js 的新手.

Kindly help. I am new to node.js.

推荐答案

请求中返回的授权代码将作为授权字段出现在标头中.这是一种使用 Node.js 将其拉出的方法.

The authorization code that will come back in requests will be in the header, as an Authorization field. Here's a way to pull it out using Node.js.

function getToken(headers) {
  // Authorization: "Bearer 123ABC"
  return headers.authorization.substr(7);
}

这篇关于如何从用于智能家居的谷歌操作中使用的 OAuth 获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆