- Mapbox Cookbook
- Bill Kastanakis
- 598字
- 2021-07-16 12:39:31
Introduction
Mapbox web services are the lowest-level API available on the platform. Every other API and functionality provided by Mapbox, such as the JavaScript API or Mapbox.js, is based on services. It uses GET REST calls to fetch the data you require from Mapbox servers.
REST services are based on HTTP verbs to perform certain actions, such as reading, creating, updating, and deleting records. GET requests are used to read data from the server; PUT requests are used usually to update data; POST requests are used to create a new record; and DELETE requests are used to delete a record. The Mapbox API is a read-only one, so we only have access to GET requests to read data.
Mapbox supports both HTTP and HTTPS secure connections. When the REST call is invalid, the server responds with the relative HTTP error code and a message in plain text, not a JSON object as usual.
Access tokens
To get access to the services, we will need an access token. We discussed access tokens in Chapter 1, Introduction to Mapbox, when we created a new project. This is a good moment to explain what an access token is.
Access tokens are unique to each account and our ticket to using Mapbox services and APIs. To be able to fetch data from Mapbox servers, the server has to identify us somehow, and providing our credentials is not a proper way to do this.
Using an access token, Mapbox knows which user is requesting data, which services are available to this user, and the bandwidth he/she uses. We are not supposed to share or publish access tokens. They should remain hidden at all costs; however, unfortunately, this is not always possible because in some cases—for example, when using JavaScript—the code, and therefore the access token, is included in the code and is easily accessible.
For this reason, Mapbox provides two different access tokens: the public access token and the secret access token. We use the public access token in places that will be exposed to the public; it is easy to access and replace. A secret access token is supposed to be used in places that remain secure and hidden, such as PHP code that will be evaluated on the server side or apps that will be compiled; changing it requires us to go through an approval process.
In the Projects dashboard, you may already have seen the public access token:

Clicking on the API access token link will open the Apps page, where you will find your secret access token:

Note
In this screen, you can find a list of applications that use the tokens.
You can also generate a new access token by clicking on the New token button in the middle of the screen. A window will open, allowing you to generate a public or secret access token, and in case of a secret token, you can define the scope.
Finding the Map ID
The second element that we need to use throughout this chapter is the Map ID. Each time we create a new map using Mapbox, it has a unique Map ID, and each time we perform a request, we have to pass the Map ID to tell Mapbox which map we are interested in.
You can find your Map ID in two different places:
- In the Projects dashboard, the Map ID is displayed directly below your map's name, and you can click on the clipboard icon next to it to copy it
- In Mapbox Editor, you can find it in the Project tab:
- 智慧城市:大數(shù)據(jù)、互聯(lián)網(wǎng)時(shí)代的城市治理(第4版)
- 通信網(wǎng)絡(luò)基礎(chǔ)與設(shè)備
- RCNP實(shí)驗(yàn)指南:構(gòu)建高級(jí)的路由互聯(lián)網(wǎng)絡(luò)(BARI)
- 連接未來(lái):從古登堡到谷歌的網(wǎng)絡(luò)革命
- C++黑客編程揭秘與防范
- EDA技術(shù)與VHDL編程
- 社交電商運(yùn)營(yíng)策略、技巧與實(shí)操
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- 數(shù)字通信同步技術(shù)的MATLAB與FPGA實(shí)現(xiàn):Altera/Verilog版(第2版)
- Mastering Dart
- 2小時(shí)讀懂物聯(lián)網(wǎng)
- Android UI Design
- 人人都該都懂的互聯(lián)網(wǎng)思維
- 5G技術(shù)核心與增強(qiáng):從R15到R17
- 現(xiàn)場(chǎng)綜合化網(wǎng)絡(luò)運(yùn)營(yíng)與維護(hù):運(yùn)營(yíng)商數(shù)字化轉(zhuǎn)型技術(shù)與實(shí)踐