使用 Mapbox Styles API 创建样式时令牌无效

问题描述 投票:0回答:0

我正在尝试使用 Mapbox 样式 API 在 Mapbox 中创建新的地图样式。我遇到了“无法处理的实体”错误,这似乎最初是由无效的访问令牌引起的。我尝试直接访问 API 的 URL,并收到消息 {"message":"Not Authorized - Invalid Token"。我使用了一个在 URL 中检查了 Styles:Write 的秘密令牌。在Mapbox token页面查看token的时候说token还没有被使用过

我是免费计划的新手。消息的原因是没有付费计划就无法访问 Styles API。或者还有其他原因导致我收到“无效令牌”错误?

这是产生错误的代码:

```
fetch(`https://api.mapbox.com/styles/v1/${username}?access_token=${accessToken}`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  },
  body: JSON.stringify(postData)
})
.then(response => {
    console.log(response); // log the response object for debugging purposes
    if (response.ok) {
      return response.json();
    } else {
       throw new Error('Error creating map style: ' + response.statusText);
    }
  })
.then(data => {
    console.log('Map style created with Map ID: ' + data.id);
    // redirect the user to the new map style URL
    const styleUrl = `https://api.mapbox.com/styles/v1/${data.owner}/${data.id}/?   access_token=${accessToken}`;
    window.location.href = styleUrl;
  })
.catch(error => {
  console.error('Error creating new style:', error);
  console.error(error.response.body);
});
```

这是变量的代码:

```
var styleJson = map.getStyle();
console.log("Map style is:",styleJson);
var newStyleName = 'TestStyle_my_name';
var postData = {
  "name": newStyleName,
  "style": styleJson
};
```

当我运行代码(发布到 URL,如上所述)时,帖子没有完成,我得到一个“不可处理的实体”错误,这似乎是从这个:“未捕获的类型错误类型错误:无法读取未定义的属性(读取'body ')"

当我直接尝试 url 链接时,出现“无效令牌”错误,因此我认为这是“无法处理的实体”错误的实际原因。

所以我的问题是:为什么,如果我创建一个带有 Styles:write 范围的秘密令牌以在 URL 请求中使用,我会收到“无效令牌”错误吗?

styles mapbox mapbox-gl-js
© www.soinside.com 2019 - 2024. All rights reserved.