小程序支付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密钥

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/news/391089.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/391089.shtml
英文地址,请注明出处:http://en.pswp.cn/news/391089.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

永无止境_永无止境地死:

永无止境Wir befinden uns mitten in der COVID-19-Pandemie und damit auch im Mittelpunkt einer medialen Geschichte, die durch eine noch nie dagewesene Komplexitt und Dynamik gekennzeichnet ist. Wie kann Informationsdesign helfen, diese Explosion von Nachrich…

HDU4612 Warm up —— 边双联通分量 + 重边 + 缩点 + 树上最长路

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid4612 Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 7206 Accepted Submission(s): 1681 Problem DescriptionN planets are …

Android sqlite load_extension漏洞解析

路人甲 2015/09/25 14:540x01 sqlite load_extensionSQLite从3.3.6版本(http://www.sqlite.org/cgi/src/artifact/71405a8f9fedc0c2)开始提供了支持扩展的能力,通过sqlite_load_extension API(或者load_extensionSQL语句&#xf…

去除Java字符串中的空格

问题:去除Java字符串中的空格 俺有一个像这样的字符串 mysz "namejohn age13 year2001";我想要去除字符串里面的空格。我尝试使用 trim() ,但是呢它只去除了字符串前后的空格。我也尝试用 ("\W", “”),但是它把也给搞…

谷歌浏览器bug调试快捷键_Bug压榨初学者指南:如何使用调试器和其他工具查找和修复Bug

谷歌浏览器bug调试快捷键As web developers, it often feels like we spend more time fixing bugs and trying to solve problems than we do writing code. In this guide well look at some common debugging techniques, so lets get stuck in.作为Web开发人员,…

吴恩达神经网络1-2-2_图神经网络进行药物发现-第1部分

吴恩达神经网络1-2-2预测溶解度 (Predicting Solubility) 相关资料 (Related Material) Jupyter Notebook for the article Jupyter Notebook的文章 Drug Discovery with Graph Neural Networks — part 2 图神经网络进行药物发现-第2部分 Introduction to Cheminformatics 化学…

再利用Chakra引擎绕过CFG

xlab 2015/12/24 15:00Author:[email protected]0x00 前言本文源自一次与TK闲聊,期间得知成功绕过CFG的经过与细节(参考:[利用Chakra JIT绕过DEP和CFG])。随即出于对技术的兴趣,也抽出一些时间看了相关的东西,结果发现了另一处绕…

论文搜索源

中国科学院文献情报中心 见下图 中国计算机学会推荐国际学术会议和期刊目录 EI学术会议中心,        engieer village 转载于:https://www.cnblogs.com/cxy-941228/p/7693097.html

重学TCP协议(10)SYN flood 攻击

1.SYN flood 攻击 SYN Flood(半开放攻击)是一种拒绝服务(DDoS)攻击,其目的是通过消耗所有可用的服务器资源使服务器不可用于合法流量。通过重复发送初始连接请求(SYN)数据包,攻击者能…

大数据入门课程_我根据数千个数据点对互联网上的每门数据科学入门课程进行了排名...

大数据入门课程by David Venturi大卫文图里(David Venturi) A year ago, I dropped out of one of the best computer science programs in Canada. I started creating my own data science master’s program using online resources. I realized that I could learn everyt…

python 数据框缺失值_Python:处理数据框中的缺失值

python 数据框缺失值介绍 (Introduction) In the last article we went through on how to find the missing values. This link has the details on the how to find missing values in the data frame. https://medium.com/kallepalliravi/python-finding-missing-values-in-…

Spring Cloud 5分钟搭建教程(附上一个分布式日志系统项目作为参考) - 推荐

http://blog.csdn.net/lc0817/article/details/53266212/ https://github.com/leoChaoGlut/log-sys 上面是我基于Spring Cloud ,Spring Boot 和 Docker 搭建的一个分布式日志系统. 目前已在我司使用. 想要学习Spring Cloud, Spring Boot以及Spring 全家桶的童鞋,可以参考学习,如…

51nod1832(二叉树/高精度模板+dfs)

题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId1832 题意: 中文题诶~ 思路: 若二叉树中有 k 个节点只有一个子树, 则答案为 1 << k. 详情参见:http://blog.csdn.net/gyhguoge01234/article/details/77836484 代码: 1 #include <iostream&g…

重学TCP协议(11)TFO(Tcp Fast Open)

1. TFO 为了改善web应用相应时延&#xff0c;google发布了通过修改TCP协议利用三次握手时进行数据交换的TFO(TCP fast open&#xff0c;RFC 7413)。 TFO允许在TCP握手期间发送和接收初始SYN分组中的数据。如果客户端和服务器都支持TFO功能&#xff0c;则可以减少建立到同一服…

[网络安全] 远程登录

远程登录方式: 1.图像化远程登录 做法: 运行"窗口"输入 "mstsc " 输入ip地址 注意: 被远程计算机&#xff0c;必须打开远程登录服务: 信息面板–系统–允许远程访问。被远程计算机&#xff0c;必须存在拥有远程桌面权限的用户。 2.命令行远程登录 teln…

外星人图像和外星人太空船_卫星图像:来自太空的见解

外星人图像和外星人太空船By Christophe Restif & Avi Hoffman, Senior Software Engineers, Crisis Response危机应对高级软件工程师Christophe Restif和Avi Hoffman Editor’s note: In 2019, we piloted a new feature in Search SOS Alerts for major California wild…

chrome恐龙游戏_如何玩没有互联网的Google Chrome恐龙游戏-在线和离线

chrome恐龙游戏Several years ago, Google added a fun little Easter egg to Chrome: if your internet went down and you tried to visit a web page, youd see the message "Unable to connect to the Internet" or "No internet" with a little pixi…

Hotpatch潜在的安全风险

屎蛋 2016/06/22 10:11author:[email protected]0x00 “Hotpatch”简介IOS App的开发者们经常会出现这类问题&#xff1a;当一个新版本上线后发现存在一个严重的bug&#xff0c;有可能因为一个逻辑问题导致支付接口存在被薅羊毛的风险&#xff0c;这个时候能做的只能是赶快修复…

spring中@Inject和@Autowired的区别?分别在什么条件下使用呢?

问题&#xff1a;spring中Inject和Autowired的区别&#xff1f;分别在什么条件下使用呢&#xff1f; 我在浏览SpringSource上的一些博客&#xff0c;在其他一个博客中&#xff0c;那个作者用了Inject&#xff0c;但是我觉得他用Autowired也行 下面是一部分代码&#xff1a; …

Objective-C语言的动态性

Objective-C具有相当多的动态特性&#xff0c;基本的&#xff0c;也是经常被提到和用到的有动态类型&#xff08;Dynamic typing&#xff09;&#xff0c;动态绑定&#xff08;Dynamic binding&#xff09;和动态加载&#xff08;Dynamic loading&#xff09; 一、编译时和运行…