<p>I have a google kubernetes cluster running and I am trying to manually scale some pods with the python-client kubernetes SDK. I use the following  command on my terminal to get my google account credentials:</p>
<pre><code>gcloud auth login
</code></pre>
<p>Next, I connect to my cluster using the default command to get locally my kube-config:</p>
<pre><code>gcloud container clusters get-credentials ${clusterName} --zone ${zoneName}--${projectName}
</code></pre>
<p>Using the python SDK I load my configuration:</p>
<pre><code>from kubernetes import client, config
import kubernetes.client

config.load_kube_config()
v1 = client.CoreV1Api()
api = client.CustomObjectsApi()
k8s_apps_v1 = client.AppsV1Api()
</code></pre>
<p>With this code I have my cluster info and I can  scale my pods as needed. This works for around 30-45 mins and after that when I try to make API requests to scale the pods in my cluster I get a response with the following error:</p>
<pre><code>kubernetes.client.exceptions.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Audit-Id': '697f82b7-4db9-46c3-b873-cef49a45bb19', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'Tue, 31 May 2022 01:20:53 GMT', 'Content-Length': '129'})
HTTP response body: {&quot;kind&quot;:&quot;Status&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{},&quot;status&quot;:&quot;Failure&quot;,&quot;message&quot;:&quot;Unauthorized&quot;,&quot;reason&quot;:&quot;Unauthorized&quot;,&quot;code&quot;:401}
</code></pre>
<p>Why do I get anauthorized and can't make API calls anymore, and how can I fix this ?</p>

thepaulbot
<p>To resolve it, you should refresh the token before calling the API. This <a href="https://github.com/kubernetes-client/python-base/blob/474e9fb32293fa05098e920967bb0e0645182d5b/config/kube_config.py#L625" rel="nofollow noreferrer">doc</a> is useful to check if the token expired, the function <code>load_gcp_token</code> refreshes the GCP token only if it expires.</p>
