<p>The parameter should come at the end of the URL:</p>
<pre><code>Invoke-RestMethod -Uri https://gitlab.com/api/v4/projects/1234567?private_token=$my_private_token
</code></pre>
<p>I prefer to pass the token in the header:</p>
<pre><code>Invoke-RestMethod -Headers @{ 'PRIVATE-TOKEN'=$_my_private_token } -Uri http://gitlab.com/api/v4/projects/1234567
</code></pre>
<p><a href="https://docs.gitlab.com/ee/api/#personalproject-access-tokens" rel="nofollow noreferrer">https://docs.gitlab.com/ee/api/#personalproject-access-tokens</a></p>

CC BY-SA 4.0
2021-12-02T15:30:11.903Z
2021-12-03T14:24:44.383Z
2021-12-03T14:24:44.383Z
swysocki
<p>I am trying to run a simple API request to GitLab through powershell 5.1. After generating an access token with all privileges using the Web UI I tried to run the following:</p>
<p><code>invoke-restmethod https://gitlab.com/api/v4/projects?private_token=&lt;my_private_token&gt;/&lt;project_id&gt;</code></p>
<p>Unfortunately, I get an error:</p>
<pre><code>invoke-restmethod : {&quot;message&quot;:&quot;401 Unauthorized&quot;}
At line:1 char:13
+ $response = invoke-restmethod https://gitlab.com/api/v4/projects?priv
...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebReques
   t:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.Pow
   erShell.Commands.InvokeRestMethodCommand
</code></pre>
<p>Why am I getting 401 error when I try to access project related information?</p>

<gitlab><api><powershell>
Trying to run a simple API GET request for private Gitlab account
user32882
# Trying to run a simple API GET request for private Gitlab account

(Asked by: username_0 on 2021-11-28)

I am trying to run a simple API request to GitLab through powershell 5.1. After generating an access token with all privileges using the Web UI I tried to run the following:
invoke-restmethod https://gitlab.com/api/v4/projects?private_token=/
Unfortunately, I get an error:
invoke-restmethod : {"message":"401 Unauthorized"}
At line:1 char:13
+ $response = invoke-restmethod https://gitlab.com/api/v4/projects?priv
...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebReques
   t:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.Pow
   erShell.Commands.InvokeRestMethodCommand

Why am I getting 401 error when I try to access project related information?

**Tags:** gitlab, api, powershell


---

## 1 answers

### Answer by username_1 on 2021-12-02. Score: 1

The parameter should come at the end of the URL:
Invoke-RestMethod -Uri https://gitlab.com/api/v4/projects/1234567?private_token=$my_private_token

I prefer to pass the token in the header:
Invoke-RestMethod -Headers @{ 'PRIVATE-TOKEN'=$_my_private_token } -Uri http://gitlab.com/api/v4/projects/1234567

https://docs.gitlab.com/ee/api/#personalproject-access-tokens


---

