【问题标题】:How can I send my Instagram access token from my Express server to React client?如何将我的 Instagram 访问令牌从我的 Express 服务器发送到 React 客户端?
【发布时间】:2017-10-31 19:26:45
【问题描述】:

我有 2 个不同的应用程序在不同的端口上运行。 1 个是 React 客户端,另一个是 Express 服务器。服务器非常简单,它只是从 Instagram 接收代码并发出 post 请求以获取访问令牌。

我获得了访问令牌,但我想将其发送回客户端,以便它可以使用它。但是客户端并没有向服务器发出请求。

流程是这样的。

客户端 -> 按下按钮验证用户

render() {
    return (
      <div>
        <button onClick={() => {
          window.open('https://api.instagram.com/oauth/authorize/?client_id=ID&redirect_uri=http://localhost:8000&response_type=code', '_blank')
        }}>Authenticate</button>
      </div>
    )
  }

然后服务器收到代码

router
.get("/", (req, res) => {
  if (req.query.code !== 'undefined') {
    axios.post('https://api.instagram.com/oauth/access_token', querystring.stringify({
      'client_id': '6',
      'client_secret': 'e',
      'grant_type': 'authorization_code',
      'redirect_uri': 'http://localhost:8000',
      'code': req.query.code
    }))
    .then(response => {
      console.log(response.data)
    })
    .catch(error => {
      console.log(error)
    })
  }
})

我的 .then 响应接收到经过身份验证的对象,但现在我被卡住了。如何在客户端中使用经过身份验证的对象?怎么寄回给客户?我应该寄回去吗?

我的客户端在 8080 端口上运行

我更愿意在我的客户端中进行所有 api 调用,因为这是我所知道的。我不太了解表达,宁愿只使用我所知道的 React / Redux。

【问题讨论】:

    标签: reactjs express authentication oauth-2.0 jwt


    【解决方案1】:

    您需要做的就是在响应中发送数据。

    为此你需要写

    res.send(response.data);
    

    【讨论】:

    • 但是我的客户没有发送请求。它会打开一个带有 Instagram 登录请求的新选项卡,并且没有来自客户端的呼叫。我会 res.send()ing 到 Instagram 吗?
    猜你喜欢
    • 2021-08-01
    • 2019-01-27
    • 2020-09-05
    • 2011-11-12
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    相关资源
    最近更新 更多