【发布时间】:2021-02-19 15:48:44
【问题描述】:
GitHub APi 文档说我需要在 header 中传递 access token。
这个命令对我来说很好用,将 OAUTH_TOKEN 替换为我的。我尝试在代码中做同样的事情:
@GET("/user/repos")
fun getAllUserRepos(
@Header("Authorization: token") accessToken: String
): Call<List<RepoJson>>
但是当我在 enqueque onFailure 中遇到错误时:头名称中的 14 处出现意外字符 0x20:授权:令牌。然后我在 header 中删除 Authorization: 和 token 之间的空格,并从 的响应中获取 Unauthorized 消息>onResponce。
我试过这个(在 header 中 Authorization: 和 token 之间也有空格):
@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)
【问题讨论】:
标签: android kotlin retrofit github-api