android retrofit2.HttpException:HTTP 401

owfi6suc  于 2023-06-20  发布在  Android
关注(0)|答案(2)|浏览(175)

谁能告诉我为什么我无法从互联网上获取数据?
我试图从www.example.com获取newsapi.orginsde我的新闻应用程序使用改装库。我不知道有什么问题。请帮帮我

NewsApiService.kt

package com.example.newsapp.network

import com.example.newsapp.model.News
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.Query

private const val BASE_URL = "https://newsapi.org/"

private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

private val retrofit = Retrofit.Builder()
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .baseUrl(BASE_URL)
    .build()

interface NewsApiService {
    @Headers("country: in", "apiKey: 4fd028e0cf3a410180f97029e7237ae7")
    @GET("v2/top-headlines")
    suspend fun getNewsHeadLines(): News

    @Headers("country: in", "apiKey: 4fd028e0cf3a410180f97029e7237ae7")
    @GET("v2/top-headlines")
    suspend fun getNewsByCategory(@Query("category") category: String): News
}

object NewsApi {
    val retrofitService: NewsApiService by lazy { retrofit.create(NewsApiService::class.java) }
}

新闻.kt

package com.example.newsapp.model

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class News(
    val status: String,
    val articles: List<Article>
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Article(
    val title: String,
    val description: String,
    val url: String,
    val urlToImage: String,
    val content: String?
)

NewsViewModel.kt

package com.example.newsapp

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.newsapp.network.NewsApi
import kotlinx.coroutines.launch

class NewsViewModel : ViewModel() {
    init {
        viewModelScope.launch {
            Log.d("adfasdf", "Here")
            try {
                val news = NewsApi.retrofitService.getNewsHeadLines()
            } catch (e: Exception) {
                Log.d("adfalkj", e.toString())
            }
        }
    }
}

这是解析的JSON响应的图像

我正在使用这个API
https://newsapi.org/docs/endpoints/top-headlines

这是错误(NewsViewModel内部的Log.d)

---------------------------- PROCESS STARTED (6024) for package com.example.newsapp ----------------------------
2023-06-19 21:34:55.993  6024-6024  adfalkj                 com.example.newsapp                  D  retrofit2.HttpException: HTTP 401

谢谢你帮忙

ttygqcqt

ttygqcqt1#

根据the documentation,API密钥需要以三种方式之一提供:

  • 作为apiKey查询参数
  • 作为X-API-Key HTTP标头
  • 使用Authentication HTTP标头

这些你都不能做。您正在使用头,但使用apiKey作为头名称。
您可以尝试:

interface NewsApiService {
    @Headers("country: in", "X-API-Key: 4fd028e0cf3a410180f97029e7237ae7")
    @GET("v2/top-headlines")
    suspend fun getNewsHeadLines(): News

    @Headers("country: in", "X-API-Key: 4fd028e0cf3a410180f97029e7237ae7")
    @GET("v2/top-headlines")
    suspend fun getNewsByCategory(@Query("category") category: String): News
}

这会将HTTP标头更改为X-API-Key,以匹配文档所要求的内容。

q3qa4bjr

q3qa4bjr2#

只需要传递带有API关键字的Header,就像方法param

@GET("...")
fun yourMethod(
  @Header key: String = YOUR_API_KEY
): Response

相关问题