Getting provider's access token
You can get the Third Party Provider's access token to query their APIs with the following method:
- NodeJS
- GoLang
- Python
import SuperTokens from "supertokens-node";import ThirdPartyPasswordless from "supertokens-node/recipe/thirdpartypasswordless";import Session from "supertokens-node/recipe/session";
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "..." }, supertokens: { connectionURI: "...", }, recipeList: [ ThirdPartyPasswordless.init({ override: { apis: (originalImplementation) => { return { ...originalImplementation,
// we override the thirdparty sign in / up API thirdPartySignInUpPOST: async function (input) { if (originalImplementation.thirdPartySignInUpPOST === undefined) { throw Error("Should never come here"); }
let response = await originalImplementation.thirdPartySignInUpPOST(input);
// if sign in / up was successful... if (response.status === "OK") { // In this example we are using Google as our provider let accessToken = response.authCodeResponse.access_token
// TODO: ... }
return response; }, } } }, }), Session.init() ]});
import ( "fmt"
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels" "github.com/supertokens/supertokens-golang/recipe/thirdpartypasswordless" "github.com/supertokens/supertokens-golang/recipe/thirdpartypasswordless/tplmodels" "github.com/supertokens/supertokens-golang/supertokens")
func main() { supertokens.Init(supertokens.TypeInput{ RecipeList: []supertokens.Recipe{ thirdpartypasswordless.Init(tplmodels.TypeInput{ Override: &tplmodels.OverrideStruct{ APIs: func(originalImplementation tplmodels.APIInterface) tplmodels.APIInterface { // First we copy the original implementation originalThirdPartySignInUpPOST := *originalImplementation.ThirdPartySignInUpPOST
// we override the thirdparty sign in / up API (*originalImplementation.ThirdPartySignInUpPOST) = func(provider tpmodels.TypeProvider, code string, authCodeResponse interface{}, redirectURI string, options tpmodels.APIOptions, userContext supertokens.UserContext) (tplmodels.ThirdPartySignInUpOutput, error) {
// first we call the original implementation resp, err := originalThirdPartySignInUpPOST(provider, code, authCodeResponse, redirectURI, options, userContext) if err != nil { return tplmodels.ThirdPartySignInUpOutput{}, err }
// if sign in / up was successful... if resp.OK != nil { authCodeResponse := resp.OK.AuthCodeResponse
accessToken := authCodeResponse.(map[string]interface{})["access_token"].(string)
fmt.Println(accessToken) // TODO: }
return resp, err }
return originalImplementation }, }, }), }, })}
from supertokens_python import init, InputAppInfofrom supertokens_python.recipe import thirdpartypasswordlessfrom supertokens_python.recipe.thirdpartypasswordless.interfaces import APIInterface, ThirdPartyAPIOptions, ThirdPartySignInUpPostOkResultfrom typing import Dict, Any, Unionfrom supertokens_python.recipe.thirdparty.provider import Provider
def apis_override(original_implementation: APIInterface): original_thirdparty_sign_in_up_post = original_implementation.thirdparty_sign_in_up_post async def thirdparty_sign_in_up_post(provider: Provider, code: str, redirect_uri: str, client_id: Union[str, None], auth_code_response: Union[Dict[str, Any], None], api_options: ThirdPartyAPIOptions, user_context: Dict[str, Any]): # First we call the original implementation of sign_in_up_post. response = await original_thirdparty_sign_in_up_post(provider, code, redirect_uri, client_id, auth_code_response, api_options, user_context)
# Post sign up response, we check if it was successful if isinstance(response, ThirdPartySignInUpPostOkResult): _ = response.user.user_id __ = response.user.email
# In this example we are using Google as our provider thirdparty_auth_response = response.auth_code_response if thirdparty_auth_response is None: raise Exception("Should never come here")
access_token = thirdparty_auth_response["access_token"]
print(access_token) # TODO
return response original_implementation.thirdparty_sign_in_up_post = thirdparty_sign_in_up_post return original_implementation
init( app_info=InputAppInfo(api_domain="...", app_name="...", website_domain="..."), framework='...', recipe_list=[ thirdpartypasswordless.init( override=thirdpartypasswordless.InputOverrideConfig( apis=apis_override ) ) ])