clerk / javascript

Official Javascript repository for Clerk authentication
https://clerk.com
MIT License
981 stars 221 forks source link

@clerk/clerk-sdk-node ClerkAPIResponseError: Unprocessable Entity - getUserOauthAccessToken google #1827

Closed AlonMiz closed 8 months ago

AlonMiz commented 9 months ago

Description

Steps to reproduce:

  1. Authorize user with Google and a gmail.send,gmail.readonly scopes
  2. refresh the browser or wait a few hours
  3. use server-side clerk.users.getUserOauthAccessToken(userId, 'oauth_google');
  4. getting ClerkAPIResponseError: Unprocessable Entity error

Expected behavior:

I would expect the code not to throw and be able to get the OAuth token, even when the last token is expired, using the refresh token it fetched in the initial authentication

Actual behavior:

The getUserOauthAccessToken mechanism seems not to handle situation when it needs to refresh the OAuth token

image

Publishable key

pk_test_cHJvcGVyLXNwYXJyb3ctNDkuY2xlcmsuYWNjb3VudHMuZGV2JA

Environment

System:
    OS: macOS 13.5.2
    CPU: (8) arm64 Apple M1 Pro
    Memory: 70.92 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.volta/tools/image/node/18.16.0/bin/node
    Yarn: 1.22.18 - ~/.volta/tools/image/yarn/1.22.18/bin/yarn
    npm: 9.5.1 - ~/.volta/tools/image/node/18.16.0/bin/npm
    pnpm: 8.5.0 - ~/Library/pnpm/pnpm
  Browsers:
    Brave Browser: 106.1.44.105
    Chrome: 117.0.5938.132
    Safari: 16.6
  npmPackages:
    @clerk/clerk-sdk-node: ^4.12.6 => 4.12.6 
    @clerk/nextjs: ^4.24.0 => 4.24.0 
    @clerk/themes: ^1.7.6 => 1.7.6 

Preliminary Checks

Reproduction / Replay Link

its pretty hard - if there's no other choice ill try to create one

jescalan commented 9 months ago

We do indeed need a reproduction in order to handle this one 🙏

statico commented 9 months ago

I'm still trying to find a reproduction here, but we noticed Clerk auth acting strangely in our React Native / Expo app starting a week ago, and we haven't updated our app in over two weeks. It might be this. I'll report back.

clerk-cookie commented 8 months ago

Hello 👋

We currently close issues after 40 days of inactivity. It's been 30 days since the last update here. If we missed this issue, please reply here. Otherwise, we'll close this issue in 10 days.

As a friendly reminder: The best way to see an issue fixed is to open a pull request. If you're not sure how to do that, please check out our contributing guide.

Thanks for being a part of the Clerk community! 🙏

clerk-cookie commented 8 months ago

Hello again 👋

After 40 days of no activity, we'll close this issue. Keep in mind, I'm just a robot, so if I've closed this issue in error, please reply here and my human colleagues will reopen it.

As a friendly reminder: The best way to see an issue fixed is to open a pull request. If you're not sure how to do that, please check out our contributing guide.

Thanks for being a part of the Clerk community! 🙏

thecrayon commented 1 month ago

@AlonMiz did you find a solution to this? I have the exact same issue as you: 1) User signs into app for first time. 2) User signs out and remains signed out for > 1 hour. 3) I go to fetch the oauth token (from server side). 4) I get the same error message as you.

Getting the access token works well when the user was recently signed in, but not if the user has been signed out for >1 hour. It's fine if I have to manage the user's refresh tokens, but I don't think the Clerk api returns the user's refresh token when the user first authenticates.

Per Clerk's documentation here Clerk says "Clerk ensures that the OAuth Access Token will be always fresh so that you don't have to worry about OAuth Refresh Tokens anymore." This doesn't appear to be the case for Google Oauth

AlonMiz commented 1 month ago

@thecrayon actually, eventually, it stopped happening... might be a version or something (I moved to clerk core v2)