https://supertokens.com/ logo
p

pvharmo

04/13/2022, 2:03 PM
Hi! I'm having an issue when i try to sign up with a thirdparty provider. -> Issue: I'm getting a FIELD_ERROR Request failed with status code 401 when I try to sign up with a Google account. What's strange is that I already managed to sign up and sign in using an other Google account. -> Frontend SDK used: supertokens-website, v10.0.8) -> Backend SDK used: supertokens-node, v9.2.1 -> Debug logs from the backend SDK (the message with log is too long for Discord so I removed the time of the request):
Copy code
{message: "middleware: requestRID is: thirdpartyemailpassword", file: "node_modules/supertokens-node/lib/build/supertokens.js:172:26" sdkVer: "9.1.2"}
{message: "middleware: Checking recipe ID for match: thirdpartyemailpassword", file: "node_modules/supertokens-node/lib/build/supertokens.js:181:34" sdkVer: "9.1.2"}
 {message: "middleware: Matched with recipe ID: thirdpartyemailpassword", file: "node_modules/supertokens-node/lib/build/supertokens.js:194:30" sdkVer: "9.1.2"}
{message: "middleware: Request being handled by recipe. ID is: /signinup", file: "node_modules/supertokens-node/lib/build/supertokens.js:206:30" sdkVer: "9.1.2"}
{message: "middleware: Started", file: "node_modules/supertokens-node/lib/build/supertokens.js:158:26" sdkVer: "9.1.2"}
{message: "middleware: requestRID is: anti-csrf", file: "node_modules/supertokens-node/lib/build/supertokens.js:172:26" sdkVer: "9.1.2"}
{message: "middleware: Checking recipe ID for match: thirdpartyemailpassword", file: "node_modules/supertokens-node/lib/build/supertokens.js:220:34" sdkVer: "9.1.2"}
{message: "middleware: Request being handled by recipe. ID is: /authorisationurl", file: "node_modules/supertokens-node/lib/build/supertokens.js:225:38" sdkVer: "9.1.2"}
{message: "Sending response to client with status code: 200", file: "node_modules/supertokens-node/lib/build/utils.js:90:14" sdkVer: "9.1.2"}

response : { status: 'FIELD_ERROR', error: 'Request failed with status code 401' }
n

nkshah2

04/13/2022, 2:07 PM
Hi, What frontend SDK are you using?
p

pvharmo

04/13/2022, 2:07 PM
it is supertokens-website, v10.0.8
I wonder, does
Request failed with status code 401
means that it is Supertokens that sent a 401 or is it Google?
r

rp

04/13/2022, 2:18 PM
which API is giving 401?
If it is the signinup API, then it's google that is returning 401
p

pvharmo

04/13/2022, 2:21 PM
the response comes from this function : originalImplementation.thirdPartySignInUpPOST(input)
r

rp

04/13/2022, 2:22 PM
right. So it's google giving a 401
p

pvharmo

04/13/2022, 2:24 PM
Ah thanks, is there a way to debug the error coming from Google because I have no idea why I'm getting an error
I mean, is there a way to see the response from Google that Supertokens-core receives?
r

rp

04/13/2022, 2:30 PM
Yea. You can put a try catch around the
originalImplementation.thirdPartySignInUpPOST(input)
and print out the error object
p

pvharmo

04/13/2022, 2:37 PM
It doesn't seems to throw anything. Here is my code:
Copy code
let response
try {
response = await originalImplementation.thirdPartySignInUpPOST(input)
} catch(e) {console.log(e)}
console.log(response)
All that is logged is
{ status: 'FIELD_ERROR', error: 'Request failed with status code 401' }
from
console.log(response)
r

rp

04/13/2022, 2:38 PM
oh hmm. So this is failing in the part for getting the user profile info.
In the place where you are providing the
providers
array, do this:
Copy code
js
let google = ThirdPartyEmailPassword.Google({
    clientId: "",
    clientSecret: "",
});
ThirdPartyEmailPassword.init({
    providers: [
        {
            ...google,
            get: function (redirectURI: string | undefined, authCodeFromRequest: string | undefined, userContext: any) {
                let getResult = google.get(redirectURI, authCodeFromRequest, userContext);
                return {
                    ...getResult,
                    getProfileInfo: async function (authCodeResponse: any, userContext: any) {
                        try {
                            return getResult.getProfileInfo(authCodeResponse, userContext);
                        } catch (err) {
                            console.log(err);
                            throw err;
                        }
                    }
                }
            }

        },
    ],
});
make sure to provide the correct
clientId
and
clientSecret
and if you see, there is a console log in the try catch that should print the full error.
Also, google is probably just returning the message "Request failed with status code 401". So the above will probably not be that helpful anyway
p

pvharmo

04/13/2022, 3:07 PM
I managed to figure it out with the last snippets you sent, that was really helpful. The issue was that I was missing the scope
https://www.googleapis.com/auth/userinfo.email
. It is probably because I set other scopes in
ThirdPartyEmailPassword.Google
and so, it didn't take the default scopes from the SDK
r

rp

04/13/2022, 3:16 PM
Can you please show me the output of that console log?
p

pvharmo

04/13/2022, 4:21 PM
Sure
Copy code
Error: Request failed with status code 401
    at createError (/home/jonathan/Documents/thinkdrive/auth/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/home/jonathan/Documents/thinkdrive/auth/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/home/jonathan/Documents/thinkdrive/auth/node_modules/axios/lib/adapters/http.js:269:11)
It also printed the config for axios request and response.
Copy code
config: {
    url: 'https://www.googleapis.com/oauth2/v1/userinfo',
    method: 'get',
    headers: {
      Accept: 'application/json, text/plain, */*',
      Authorization: 'Bearer ya29.A0ARrdaM86A9IMFJKHehpYCx372RggXa8Pf7hakMe7SA9vuKRG-4aUO_cz-OmdFyUV1tIPliEXf3AeRkzCGQKpok0L8GMTIgMePClv1C88BFrtf7WZZt2669P-fI46935M6Hq0kDtjWMDWjPJB-y1kz6aGDCyh',
      'User-Agent': 'axios/0.21.4'
    },
...
It's like you said though, there wasn't much more information in the error, but knowing the url for the request which caused the error helped a lot.
r

rp

04/13/2022, 4:21 PM
ah i see! ok fair. I wish the oauth providers would be more verbose with what went wrong
p

pvharmo

04/13/2022, 4:23 PM
Yes, I wish that too. Still, thanks for your help.
2 Views