小程序支付api密钥

问题 (The Problem)

All you want to do is fetch some JSON from an API endpoint for the weather, some book reviews, or something similarly simple.

您要做的就是从API端点获取一些有关天气的JSON,一些书评或类似的简单内容。

The fetch query in your front-end is easy enough, but you have to paste your secret API key right there in the front-end code for anybody to find with a trivial amount of digging!

前端中的获取查询非常容易,但是您必须在前端代码中将您的秘密API密钥粘贴到前端代码中,以便任何人都能通过少量的挖掘找到它!

Also, pushing your API keys to your GitHub repository is a major problem: Dev put AWS keys on Github. Then BAD THINGS happened.

另外,将API密钥推送到GitHub存储库也是一个主要问题: Dev将AWS密钥放在Github上。 然后发生了坏事 。

"Why is this so hard?!" – You, probably 15 minutes ago

“为什么这么难?!” –您,大约15分钟前

解决方案 (The Solution)

You should use a back-end server as a relay to fetch the API results for you and then pass them on to your front-end

您应该使用后端服务器作为中继来为您获取API结果,然后将其传递给您的前端

新问题 (The New Problem)

You're just trying to do a front-end demo for your portfolio! You haven't learned anything about back-end technologies yet! Why is this so hard?!

您只是想为您的投资组合做一个前端演示! 您尚未了解有关后端技术的任何信息! 为什么这么难?!

演示版 (Demo)

I've encountered this problem often enough that I've decided to stop coming up with silly hacks and implement a solution that works with minimal back-end code.

我经常遇到此问题,以至于我决定停止提出愚蠢的骇客,并实施一个使用最少的后端代码的解决方案。

In this demo I set up a back-end that listens for POST requests and sends them to the GoodReads API. To use this you need to implement your own front-end that can send the appropriate POST request to this back-end. Your front-end won't communicate with GoodReads directly, so no API key is exposed.

在此演示中,我设置了一个后端,用于侦听POST请求并将其发送到GoodReads API 。 要使用此功能,您需要实现自己的前端,该前端可以将适当的POST请求发送到此后端。 您的前端不会直接与GoodReads通信,因此不会暴露任何API密钥。

你会需要 (You will need)

  • Node (this has been tested with v10.16.0, later versions will be fine, earlier ones may encounter problems)

    节点 (已通过v10.16.0进行了测试,以后的版本会很好,早期的版本可能会遇到问题)

  • git

    吉特

  • This repo: https://github.com/JacksonBates/example-goodreads-api-relay这个仓库:https://github.com/JacksonBates/example-goodreads-api-relay

开始吧 (Get started)

git clone https://github.com/JacksonBates/example-goodreads-api-relay.git

git clone https://github.com/JacksonBates/example-goodreads-api-relay.git

The README.md contains everything you should need to know, including installation and set up.

README.md包含您需要了解的所有内容,包括安装和设置。

I've included the key points here for convenience:

为了方便起见,我在此处列出了关键点:

自述文件 (README.md)

Install dependancies:

安装依赖关系:

npm i

npm i

You need to create your own .env file for your key:

您需要为密钥创建自己的.env文件:

cp .env.example .env

cp .env.example .env

Then open the new .env file and paste your keys in the correct spot.

然后打开新的.env文件,然后将密钥粘贴到正确的位置。

Example:

例:

GOODREADS_API_KEY=AABBCCDDEEFF00112233445566778899

Now run the server:

现在运行服务器:

node app.js

node app.js

In the browser, navigate to localhost:3000 to confirm the server is running. You should see a simple Hello World!

在浏览器中,导航到localhost:3000以确认服务器正在运行。 您应该会看到一个简单的Hello World!

接下来是什么? (What next?)

Now read the app.js file thoroughly.

现在,彻底阅读app.js文件。

I've commented the code heavily to help you understand what is going on if you haven't seen node / express much before.

我对代码进行了重注释,以帮助您了解以前没有多少节点/表达式的情况。

// app.js// These import necessary modules and set some initial variables
require("dotenv").config();
const express = require("express");
const fetch = require("node-fetch");
const convert = require("xml-js");
const rateLimit = require("express-rate-limit");
const app = express();
const port = 3000;// Rate limiting - Goodreads limits to 1/sec, so we should too// Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
// see https://expressjs.com/en/guide/behind-proxies.html
// app.set('trust proxy', 1);const limiter = rateLimit({windowMs: 1000, // 1 secondmax: 1, // limit each IP to 1 requests per windowMs
})//  apply to all requests
app.use(limiter)// Routes// Test route, visit localhost:3000 to confirm it's working
// should show 'Hello World!' in the browser
app.get("/", (req, res) => res.send("Hello World!"));// Our Goodreads relay route!
app.get("/api/search", async (req, res) => {try {// This uses string interpolation to make our search query string// it pulls the posted query param and reformats it for goodreadsconst searchString = `q=${req.query.q}`;// It uses node-fetch to call the goodreads api, and reads the key from .envconst response = await fetch(`https://www.goodreads.com/search/index.xml?key=${process.env.GOODREADS_API_KEY}&${searchString}`);//more info here https://www.goodreads.com/api/index#search.booksconst xml = await response.text();// Goodreads API returns XML, so to use it easily on the front end, we can// convert that to JSON:const json = convert.xml2json(xml, { compact: true, spaces: 2 });// The API returns stuff we don't care about, so we may as well strip out// everything except the results:const results = JSON.parse(json).GoodreadsResponse.search.results;return res.json({success: true,results})} catch (err) {return res.status(500).json({success: false,message: err.message,})}
})// This spins up our sever and generates logs for us to use.
// Any console.log statements you use in node for debugging will show up in your
// terminal, not in the browser console!
app.listen(port, () => console.log(`Example app listening on port ${port}!`));

Update: Huge thanks to Gouri Shankar Kumawat for contributing a PR that improved this code! You can follow him on Twitter at @dev_gskumawat, or on GitHub: gskumawat0

更新 :非常感谢Gouri Shankar Kumawat贡献了改进此代码的PR! 您可以在Twitter上@dev_gskumawat或在GitHub上关注他: gskumawat0

测试API中继 (Test the API relay)

Use Postman to test the API.

使用Postman测试API。

Set Postman to GET and paste this in the url: localhost:3000/api/search?q=hobbit

将Postman设置为GET并将其粘贴在url中: localhost:3000/api/search?q=hobbit

Postman will show you the JSON response below.

邮递员将在下面显示JSON响应。

您如何在前端使用它? (How do you use this in your front end?)

This simple app is listening for post requests at /api/search, so interact with it in your front end app the way you have been previously with the original api.

这个简单的应用程序正在/api/search监听发布请求,因此可以像以前使用原始api的方式在前端应用程序中与之交互。

This is only configured to handle search queries - if you want to use other Goodreads API endpoints / methods, you'll need to think about how you implement them yourself!

它仅配置为处理搜索查询-如果您想使用其他Goodreads API端点/方法,则需要考虑如何自己实现它们!

代管 (Hosting)

You can't deploy your front-end and still have this on localhost - obviously you need to deploy this, too.

您无法部署前端,而仍在本地主机上拥有它-显然您也需要部署它。

I recommend Heroku.

我推荐Heroku 。

额外信用 (Extra Credit)

If you wanted to extend this, you could consider how you might only make this accessible from a restricted range of IP addresses to increase the security - which was outside of the scope of this tutorial / demo.

如果要扩展此功能,可以考虑如何仅允许从有限的IP地址范围访问此地址,以提高安全性-这超出了本教程/演示的范围。



This was hastily put together in response to a discussion on the forum. If you spot any issues in this post or the example code, please don't hesitate to reply to the forum thread that started it all. I'll keep the article and repo up-to-date with improvements.

这是为了响应论坛上的讨论而匆忙进行的。 如果您发现本文或示例代码中有任何问题,请随时回复启动所有内容的论坛主题 。 我将继续撰写本文,并回购最新的改进内容。

Feel free to submit PRs if you have valuable contributions to make :)

如果您有宝贵的贡献,请随时提交PR:

You can also reach out to me via Twitter: @JacksonBates.

您也可以通过Twitter: @JacksonBates与我联系 。

翻译自: https://www.freecodecamp.org/news/private-api-keys/

小程序支付api密钥

小程序支付api密钥_如何避免在公共前端应用程序中公开您的API密钥相关推荐

  1. 微信小程序支付java视频_【原创】微信小程序支付(普通模式,公众号支付同适用)java后台案例...

    /*** * 支付回调接口*/@RequestMapping("/userpaycallback")publicString wxUserPaycallback(HttpServl ...

  2. 小程序 iphone和安卓_如何阻止iPhone和iPad应用程序要求评级

    小程序 iphone和安卓 Lots of iPhone and iPad apps ask for ratings, and they often don't stop. Even if you d ...

  3. 小程序转换纯数字数字_低代码,快速的应用程序开发和数字转换

    小程序转换纯数字数字 最近,许多低码/无码解决方案在企业中得到了发展,使非技术人员可以选择创建简单的应用程序. 分析人士预测,低码行业将以每年20%以上的速度增长. 但是什么是低码,为什么它如此流行, ...

  4. python百度地图api经纬度_详解用Python调用百度地图正/逆地理编码API

    一.背景 (正)地理编码指的是:将地理位置名称转换成经纬度: 逆地理编码指的是:将经纬度转换成地理位置信息,如地名.所在的省份或城市等 百度地图提供了相应的API,可以方便调用.相应的说明文档如下: ...

  5. api质量等级_【图】机油参数如何选? 细说机油的API质量等级_汽配中国网

    除了汽油,车主们最关注的车用油液就是发动机机油了.但对于普通车主来说,在选用机油的时候经常感到困惑--究竟什么样的机油适合我的车呢?有没有更好的机油能提高我的发动机动力降低噪音呢?换油里程究竟是多少公 ...

  6. swing程序 过时拉嘛_从关闭或过时的应用程序导入文件

    swing程序 过时拉嘛 使用专有应用程序的最大风险之一是,如果软件消失或终止对旧文件格式的支持,将会失去对数字内容的访问权限. 将您的内容移动为开放格式是防止自己由于供应商锁定而被锁定的最佳方法,为 ...

  7. 刷程序对车危害_刷ecu非常后悔,刷程序对车危害有多大

    相信大家都听说过手机刷机,那你有没有了解过发动机刷ecu呢?很多改装车迷都热衷于刷ecu,但还有很多车主刷ecu非常后悔,这到底是为什么呢,刷程序对车危害有多大?下面就一起来看看吧. 汽车ecu就像手 ...

  8. python语言程序设计基础考试题库_中国大学MOOC(慕课)_Python语言程序设计基础_测试题及答案...

    该图为我国某史前文明遗址某原始村落的平面复原示意图,请根据图中提供的相关信息,完成16-17题.小题1:下列关 图为河谷中的四个乡村聚落,其中最有可能发展为城市的是A.aB.bC.cD.d 自然条件对 ...

  9. 编写java程序计算梯形面积_【Java】编写一个应用程序计算梯形和圆形的面积。...

    说明:这是武汉理工大学计算机学院[Java语言程序设计]课程实验1:编写一个应用程序计算梯形和圆形的面积. >>点击查看WUTer计算机专业实验汇总 谨记:纸上得来终觉浅,绝知此事要躬行. ...

最新文章

  1. Linux正则表达式grep与egrep
  2. C# WebRequest 基础连接已关闭 连接意外关闭
  3. ITK:从Seed开始迭代图像
  4. Java基础(三十五)Math、Random类和数字格式化(String.format方法)
  5. MySQL性能优化的最佳经验
  6. EMR 配置纪录(不断更新)
  7. 计算机操作系统详细学习笔记(四):设备管理 —— I/O 管理
  8. Power Strings POJ - 2406,字符串hash
  9. 探讨【IGE】的源代码【二】。
  10. Mac如何取消远程控制?
  11. 细数门店客流量统计的那些技术
  12. 双目测距 SGBM算法 Python版
  13. 窝在二线城市很难受,要杀回一线城市重造吗?
  14. 【mysql】MySQL中的锁原理(表锁、行锁、间隙锁、共享锁、排他锁)
  15. canvas 擦除动画_HTML5 canvas橡皮擦擦拭效果
  16. 知识付费小程序源码可开流量主
  17. imx6 Android gpu 内存,IMX8M / IMX8M NANO /IMX6D 等GPU的简单对比
  18. 微信公众号查询账户余额等
  19. leetcode 滑动窗口
  20. 西数硬盘砍头流程说明

热门文章

  1. 【C++ Priemr | 15】虚函数表剖析(三)
  2. Apache Tomcat 7 Configuration BIO NIO AIO APR ThreadPool
  3. Squid 访问控制配置
  4. node.js安装部署测试
  5. koa2 中使用 svg-captcha 生成验证码
  6. django的contenttype表
  7. 10分钟腾讯云配置免费https
  8. Django运维后台的搭建之四:用bootstrap模板让运维前台变得更漂亮
  9. 《Android 应用案例开发大全(第二版)》——导读
  10. java日期的运用(DateUtils工具类)