当前位置: 首页 > 知识库问答 >
问题:

从服务器收到状态代码401:Unauthorized react native mapbox

海翔宇
2023-03-14

我得到一个授权错误时使用mapbox在反应本机

https://api.mapbox.com/downloads/v1/navigation/android/maven/com/mapbox/navigation/ui/1.5.0/ui-1.5.0.pom'. 从服务器收到状态代码401:未经授权

我正在使用的包(https://www.npmjs.com/package/@home e/react-native-mapbox导航)

根据软件包文档,我也在这样做。

我的build.gradle档案是

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "29.0.3"
        minSdkVersion = 21
        compileSdkVersion = 29
        targetSdkVersion = 29
        ndkVersion = "20.1.5948944"
    }
    repositories {
        google()
        jcenter()
          maven {
          url 'https://api.mapbox.com/downloads/v1/navigation/android/maven'
          authentication {
            basic(BasicAuthentication)
          }
          credentials {
            username = "mapbox"
            password = project.properties['MAPBOX_DOWNLOADS_TOKEN'] ?: "" 
          }
        }
    }
    dependencies {
        classpath("com.android.tools.build:gradle:4.1.0")
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }

        google()
        jcenter()
        maven { url 'https://mapbox.bintray.com/mapbox' }
        maven { url 'https://www.jitpack.io' }
        maven {
          url 'https://api.mapbox.com/downloads/v1/navigation/android/maven'
          authentication {
            basic(BasicAuthentication)
          }
          credentials {
            username = "mapbox"
            password = project.properties['MAPBOX_DOWNLOADS_TOKEN'] ?: "" 
          }
        }
    }
}

共有1个答案

段干宾白
2023-03-14

我最近遇到了一个类似的问题。我的第一个建议是,如果您正在使用他们的公共访问令牌,请从访问令牌创建一个新的令牌,并确保选中下载:读取范围。然后创建渐变。属性文件,并粘贴以下行:

MAPBOX_DOWNLOADS_TOKEN=YOUR_SECRET_ACCESS_KEY

当然,您应该用实际的访问密钥替换您的_SECRET\u ACCESS\u密钥。以防Android Studio无法找到这个新创建的gradle。属性文件中,我建议将上述行的副本添加到您的项目gradle中。属性文件[这是修复我的问题的方法]虽然我不确定这是否有任何安全隐患。最后是你的构建。gradle应包含以下内容:

maven {
        url 'https://api.mapbox.com/downloads/v2/releases/maven'
        authentication {
            basic(BasicAuthentication)
        }
        credentials {
            // Do not change the username below.
            // This should always be `mapbox` (not your username).
            username = 'mapbox'
            // Use the secret token you stored in gradle.properties as the password
            password = System.getenv("MAPBOX_DOWNLOADS_TOKEN") ?: project.property("MAPBOX_DOWNLOADS_TOKEN") as String
        }
    }
 类似资料: