无法将 GitHub API header "Authorization token" 添加到 Retrofit2 中的 GET 请求
Cannot add GitHub API header "Authorization token" to GET request in Retrofit2
GitHub APi 文档说我需要在 header 中传递 access token .
这个命令对我来说很好用,将 OAUTH_TOKEN 替换为我的。我尝试在代码中做同样的事情:
@GET("/user/repos")
fun getAllUserRepos(
@Header("Authorization: token") accessToken: String
): Call<List<RepoJson>>
但是当我在 enqueque onFailure 中出错时:header 名称中的 14 处出现意外字符 0x20:授权:令牌。然后我删除 space remove space between Authorization: and token in header 并从 onResponce.
中的响应中获取 Unauthorized 消息
我试过这个(也在 Authorization: 和 [=56 中的 token 之间使用 space =]):
@Headers("Authorization: token MY_VALID_TOKEN")
@GET("/user/repos")
fun getAllUserRepos(): Call<List<RepoJson>>
并且执行成功(通过入队)。
我的API:
val api: GiHubApi = Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(GitHubApi::class.java)
根据文档,
key="Authorization" and value="token VALID_TOKEN"
在改造中,
您需要将密钥传递给 @GET
。但是您传递的是“授权:令牌”
所以你需要这样做。
@GET("/user/repos")
fun getAllUserRepos(
@Header("Authorization") accessToken: String
): Call<List<RepoJson>>
致电
getAllUserRepos("token $VALID_TOKEN")
header应该是:
@GET("/user/repos")
fun getAllUserRepos(
@Header("Authorization") accessToken: String
): Call<List<RepoJson>>
去过那里,完成了 that。
GitHub APi 文档说我需要在 header 中传递 access token .
这个命令对我来说很好用,将 OAUTH_TOKEN 替换为我的。我尝试在代码中做同样的事情:
@GET("/user/repos")
fun getAllUserRepos(
@Header("Authorization: token") accessToken: String
): Call<List<RepoJson>>
但是当我在 enqueque onFailure 中出错时:header 名称中的 14 处出现意外字符 0x20:授权:令牌。然后我删除 space remove space between Authorization: and token in header 并从 onResponce.
中的响应中获取 Unauthorized 消息我试过这个(也在 Authorization: 和 [=56 中的 token 之间使用 space =]):
@Headers("Authorization: token MY_VALID_TOKEN")
@GET("/user/repos")
fun getAllUserRepos(): Call<List<RepoJson>>
并且执行成功(通过入队)。
我的API:
val api: GiHubApi = Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(GitHubApi::class.java)
根据文档, key="Authorization" and value="token VALID_TOKEN"
在改造中,
您需要将密钥传递给 @GET
。但是您传递的是“授权:令牌”
所以你需要这样做。
@GET("/user/repos")
fun getAllUserRepos(
@Header("Authorization") accessToken: String
): Call<List<RepoJson>>
致电
getAllUserRepos("token $VALID_TOKEN")
header应该是:
@GET("/user/repos")
fun getAllUserRepos(
@Header("Authorization") accessToken: String
): Call<List<RepoJson>>
去过那里,完成了 that。