1

I'm using Google Cloud API to fetch some posts from Blogger into my android app. I'm using Volly for requesting. All working fine when there is no Restriction to the API key. But when I Restrict my API key for Android, it stops working and I'm getting:

E/Volley: [89773] NetworkUtility.shouldRetryException: Unexpected response code 403 for https://www.googleapis.com/blogger/v3/blogs/[my blog id]/posts/search?q=[my query]&key=[my API key]    
E/onErrorResponse:: com.android.volley.AuthFailureError

Also, I have cross-checked my package name and SHA-1 Key and all are correct.

If I remove the restriction now it again working fine. But I want to Restrict it to my app itself.

How to restrict my key for android in the google cloud console... any help..?

Venkat
  • 384
  • 1
  • 16

1 Answers1

0

I was facing the same issue below is the working code, You just have to override the headers of the request

class MainActivity : AppCompatActivity(), NewsItemClicked {
    private lateinit var mAdapter: NewsListAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView.layoutManager = LinearLayoutManager(this)
        fetchData()
        mAdapter = NewsListAdapter(this)
        recyclerView.adapter = mAdapter
    }

    private fun fetchData(){
      val url = "https://newsapi.org/v2/top-headlines?country=in&apiKey=b85b8dedeaaa4cd391d2e211ca5faf96"
        val jsonObjectRequest = object: JsonObjectRequest(
            Request.Method.GET,
            url,
            null,
            Response.Listener {
                val newsJsonArray = it.getJSONArray("articles")
                val newsArray = ArrayList<News>()
                for(i in 0 until newsJsonArray.length()){
                    val newsJsonObject = newsJsonArray.getJSONObject(i)
                    val news = News(
                        newsJsonObject.getString("title"),
                        newsJsonObject.getString("author"),
                        newsJsonObject.getString("url"),
                        newsJsonObject.getString("urlToImage"),
                    )
                    newsArray.add(news)
                }

                mAdapter.updatedNews(newsArray)
            },
            Response.ErrorListener {
                Log.e("ERRORFETCH", "Something went wrong in fething")
            },
        )
        {
            override fun getHeaders(): MutableMap<String, String> {
            val headers = HashMap<String, String>()
            headers["User-Agent"] = ": Mozilla/5.0"
            return headers
        }
        }
        MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)
    }

    override fun onItemClicked(item: News) {

    } }
  • Thanks for Your response. But here I'm dealing with Google Cloud API and problem with its restriction to android. I think adding `headers["User-Agent"] = ": Mozilla/5.0"` to my headers will not solve my problem. – Venkat Aug 14 '21 at 16:32
  • well @Venkat you might be missing some headers, like this error comes when the api is expecting some headers in req. – Chirag Kushwaha Aug 14 '21 at 17:34
  • I had seen documentation in google's official pages and passing headers accordingly. Also it working fine if there is no restriction to that API until I set a restriction to work only in Android. – Venkat Aug 16 '21 at 12:53