Notification texts go here Contact Us Buy Now!

Getting a 401 Error with Github API and Apollo Client Yet It previously worked

If you're utilizing the Github API with Apollo Client and unexpectedly encounter a 401 error despite the request previously working, there are a few potential causes and solutions to consider:

  1. Expired Token:

    Review the token used for authentication. It's possible that the token has expired or been revoked. Create a new token and try again.

  2. Leaked Token:

    Ensure that sensitive information like your GitHub token has not been inadvertently published. If it has been, create a new token to resolve the issue.

  3. Apollo Client Configuration:

    Double-check the Apollo Client configuration, including the server URL, HTTP headers, and token placement. Refer to the code snippets below for guidance:

    • Apollo 3:
      val apolloClient= ApolloClient.Builder()
              .serverUrl("YourDamin")
              .addHttpHeader("Accept","application/json")
             
              .addHttpHeader(AUTHORIZATION, token!!)
              .build()
      
      
          lifecycleScope.launchWhenResumed {
      
              try {
                  val response = apolloClient.query(UserQuery()).execute()
                  if (response.hasErrors()) {
      
                  } else {
                    // response==>
                  }
      
              }catch (e:com.apollographql.apollo3.exception.ApolloHttpException){
      
                  try {
                      if (e.statusCode == 401) {
                        // true
                      }
                  } catch (e: JSONException) {
                      e.printStackTrace()
                  }
              }
      
          }
      

      Apollo 2:

      viewModelScope.launch {
              try {
                  val response = apolloClient.query(AddressesQuery(page)).execute()
                  //this lien add and get statusCode in catch
                  response.dataOrThrow() 
                  
                  if (response.hasErrors()) {
                      Constants.toastShort(context, response.errors!![0].message)
                  } else {
                  
                      
      
                     
                  }
              } catch (e: ApolloHttpException) {
                  Constants.apolloError(e, context)
              } catch (e: ApolloNetworkException) {
                  Constants.toastCheckNetwork(context)
              }
          }
      

While troubleshooting, it's essential to consult the documentation provided by Github API and Apollo Client to ensure that your implementation adheres to the guidelines and best practices. Additionally, checking for updates and announcements from Github and Apollo can help you stay informed about changes that might affect your integration.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.