# Server-to-Server OAuth returns 400 when requesting access token - Powershell

**URL:** <https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999>\
**Category:** API and Webhooks\
**Tags:** oauth, api\
**Created:** [October 5, 2022, 11:47pm UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999 "2022-10-05T23:47:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoom123](https://avatars.discourse-cdn.com/v4/letter/z/85e7bf/32.png) [@zoom123](https://devforum.zoom.us/u/zoom123)\
**Post date:** [October 5, 2022, 11:47pm UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/1 "2022-10-05T23:47:17Z")

</div>

**API Endpoint**  
_[https://zoom.us/oauth/token?grant\_type=account\_credentials&account\_id=a12b3c4d5e6f](https://zoom.us/oauth/token?grant_type=account_credentials&account_id=a12b3c4d5e6f)_

**Description**

I read the docs, cannot figure this out. Using Powershell, when I send a POST containing the header “Authorization: Basic {Base64 Encoded client\_id:client\_secret}” and to the endpoint above (using the account\_id on the same page from the App Credentials), I always get a 400 Bad Request.

I know I can call REST APIs using this same/similar code, because in the same script I’m making successful calls to the Microsoft Graph API.

I tried disabling and re-enabling the app, regenerating the client secret, passing ContentType as _application/json_ and _application/x-www-form-urlencoded_, even used curl _exactly_ as it’s shown in the docs - no dice.  
``  
**Example Code**

`$zoomAuthSecret = "client_id:client_secret" $zasBytes = [System.Text.Encoding]::Unicode.GetBytes($zoomAuthSecret) $EncodedZas = [Convert]::ToBase64String($zasBytes)`

`Invoke-RestMethod -Headers @{Authorization = "Basic $EncodedZas"} -Uri "https://zoom.us/oauth/token?grant_type=account_credentials&account_id={account_id}" -Method Post`

---

<div class="post-metadata">

**Author:** ![donte.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/donte.zoom/32/24412_2.png) [@donte.zoom](https://devforum.zoom.us/u/donte.zoom)\
**Post date:** [October 6, 2022, 12:15am UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/2 "2022-10-06T00:15:54Z")

</div>

Hi, @zoom123,

Thank you for posting in the Developer Forum – I am happy to help. Sorry to hear that you getting 400 bad request. Can you try to make the request with the following curl command :

```auto
curl --location --request POST 'https://zoom.us/oauth/token?grant_type=account_credentials&account_id={accountId}' \
--header 'Authorization: Basic Base64Encoder(clientId:clientSecret)' \
--data-raw ''

```

Also, there is a helpful utility script to generate a Zoom Server-to-Server Oauth Token and copy it to the clipboard on our GitHub as well:

> **[GitHub - zoom/Server-to-Server-Oauth-Token: Utility script to generate a Zoom...](https://github.com/zoom/Server-to-Server-Oauth-Token)**
>
> Utility script to generate a Zoom Server-to-Server Oauth Token and copy to clipboard - GitHub - zoom/Server-to-Server-Oauth-Token: Utility script to generate a Zoom Server-to-Server Oauth Token and...

---

<div class="post-metadata">

**Author:** ![zoom123](https://avatars.discourse-cdn.com/v4/letter/z/85e7bf/32.png) [@zoom123](https://devforum.zoom.us/u/zoom123)\
**Post date:** [October 6, 2022, 12:39am UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/3 "2022-10-06T00:39:55Z")

</div>

That worked on curl! But that begs the question, why does that not work in powershell?

---

<div class="post-metadata">

**Author:** ![donte.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/donte.zoom/32/24412_2.png) [@donte.zoom](https://devforum.zoom.us/u/donte.zoom)\
**Post date:** [October 6, 2022, 12:44am UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/4 "2022-10-06T00:44:51Z")

</div>

@zoom123,

Can you try this Powershell RestMethod and share the result:

```auto
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic Base64Encoder(clientId:clientSecret)")

$body = ""

$response = Invoke-RestMethod 'https://zoom.us/oauth/token?grant_type=account_credentials&account_id={accountId}' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

```

---

<div class="post-metadata">

**Author:** ![zoom123](https://avatars.discourse-cdn.com/v4/letter/z/85e7bf/32.png) [@zoom123](https://devforum.zoom.us/u/zoom123)\
**Post date:** [October 6, 2022, 1:05pm UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/5 "2022-10-06T13:05:13Z")

</div>

```auto
$zoomAuthSecret = "client_id:client_secret"
$zasBytes = [System.Text.Encoding]::Unicode.GetBytes($zoomAuthSecret)
$EncodedZas =[Convert]::ToBase64String($zasBytes)

$zoomheaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$zoomheaders.Add("Authorization", "Basic $EncodedZas")

$body = ""

$response = Invoke-RestMethod 'https://zoom.us/oauth/token?grant_type=account_credentials&account_id={account_id}' -Method 'POST' -Headers $zoomheaders -Body $body

$response | ConvertTo-Json

```

This unfortunately still returns a 400.

---

<div class="post-metadata">

**Author:** ![MultiplayerSession](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/multiplayersession/32/66446_2.png) [@MultiplayerSession](https://devforum.zoom.us/u/MultiplayerSession)\
**Post date:** [October 6, 2022, 3:12pm UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/6 "2022-10-06T15:12:51Z")

</div>

Some potential areas you may want to check:

- Use a single-byte encoding like ASCII or UTF-8.
- ~~Encode using base64url encoding, which replaces certain characters and discards padding.~~ Actually, my current Server-to-Server OAuth implementation is using base64 encoding; it was JWT that used base64url encoding.

~~A .NET Framework / PowerShell compatible implementation for both of the above might be available via [Microsoft.IdentityModel.Tokens.Base64UrlEncoder.Encode](https://learn.microsoft.com/en-us/dotnet/api/microsoft.identitymodel.tokens.base64urlencoder.encode).~~

---

<div class="post-metadata">

**Author:** ![zoom123](https://avatars.discourse-cdn.com/v4/letter/z/85e7bf/32.png) [@zoom123](https://devforum.zoom.us/u/zoom123)\
**Post date:** [October 6, 2022, 4:18pm UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/7 "2022-10-06T16:18:45Z")

</div>

Looks like encoding was the issue! When converting the string above to bytes, I was converting it using Unicode:

```auto
$zasBytes = [System.Text.Encoding]::Unicode.GetBytes($zoomAuthSecret)

```

But if I change it to **UTF8** :

```auto
$zasBytes = [System.Text.Encoding]::UTF8.GetBytes($zoomAuthSecret)

```

Success!

Thank you so much

---

<div class="post-metadata">

**Author:** ![donte.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/donte.zoom/32/24412_2.png) [@donte.zoom](https://devforum.zoom.us/u/donte.zoom)\
**Post date:** [October 6, 2022, 4:48pm UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/8 "2022-10-06T16:48:44Z")

</div>

> [@zoom123](#):
>
> Looks like encoding was the issue! When converting the string above to bytes, I was converting it using Unicode:

Awesome, glad to hear you were able to solve the issue. Can you share a snippet of both the failed and successful requests? It will be a great benefit to the community.

---

<div class="post-metadata">

**Author:** ![zoom123](https://avatars.discourse-cdn.com/v4/letter/z/85e7bf/32.png) [@zoom123](https://devforum.zoom.us/u/zoom123)\
**Post date:** [October 6, 2022, 5:34pm UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/9 "2022-10-06T17:34:05Z")

</div>

**Failed response using unicode encoding for Base64 Credentials**

```auto
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:10 char:13
+ $response = Invoke-RestMethod 'https://zoom.us/oauth/token?grant_type ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

```

**Successful response using UTF8 encoding for Base64 Credentials**

_access\_token and scope redacted_ \*

```auto
{
    "access_token": "",
    "token_type": "bearer",
    "expires_in": 3599,
    "scope": ""
}

```

---

<div class="post-metadata">

**Author:** ![donte.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/donte.zoom/32/24412_2.png) [@donte.zoom](https://devforum.zoom.us/u/donte.zoom)\
**Post date:** [October 6, 2022, 5:59pm UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/10 "2022-10-06T17:59:50Z")

</div>

Thanks for taking the time to share your solution with the community, @zoom123 !

```auto
$zoomAuthSecret = "client_id:client_secret"

$zasBytes = [System.Text.Encoding]::UTF8.GetBytes($zoomAuthSecret)
$EncodedZas =[Convert]::ToBase64String($zasBytes)

$zoomheaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$zoomheaders.Add("Authorization", "Basic $EncodedZas")

$body = ""

$response = Invoke-RestMethod 'https://zoom.us/oauth/token?grant_type=account_credentials&account_id={account_id}' -Method 'POST' -Headers $zoomheaders -Body $body

$response | ConvertTo-Json

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/zoomdeveloper/original/3X/6/1/614bdd549b610bbaa46ff934617683a02bdaa03c.png) [@system](https://devforum.zoom.us/u/system)\
**Post date:** [October 10, 2023, 9:36am UTC](https://devforum.zoom.us/t/server-to-server-oauth-returns-400-when-requesting-access-token-powershell/76999/11 "2023-10-10T09:36:28Z")

</div>

This topic was automatically closed 368 days after the last reply. New replies are no longer allowed.
