<p>I've created a service account for CI purposes and am testing it out. Upon trying any kubectl command, I get the error:</p>

<pre><code>error: You must be logged in to the server (Unauthorized)
</code></pre>

<p>Below is my <code>.kube/config</code> file</p>

<pre><code>apiVersion: v1
clusters:
- cluster:
    server: &lt;redacted&gt;
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: bamboo
  name: default
current-context: 'default'
kind: Config
preferences: {}
users:
- name: bamboo
  user:
    token: &lt;redacted&gt;

</code></pre>

<p>The service account exists and has a cluster role: edit and cluster role binding attached.</p>

<p>What am I doing wrong?</p>

<p>I reproduce the error if I copy the <code>token</code> directly without decoding. Then applied  the following steps to decode and set the token and it is working as expected.</p>

<pre><code>
$ TOKENNAME=`kubectl -n &lt;namespace&gt; get serviceaccount/&lt;serviceaccount-name&gt; -o jsonpath='{.secrets[0].name}'` 

$ TOKEN=`kubectl -n &lt;namespace&gt; get secret $TOKENNAME -o jsonpath='{.data.token}'| base64 --decode`

$ kubectl config set-credentials &lt;service-account-name&gt; --token=$TOKEN

</code></pre>

<p>So, I think it might be your case.</p>
