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 ThirdParty from "supertokens-node/recipe/thirdparty";import Session from "supertokens-node/recipe/session";
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "..." }, supertokens: { connectionURI: "...", }, recipeList: [ ThirdParty.init({ signInAndUpFeature: { providers: [/* ... */] }, override: { apis: (originalImplementation) => { return { ...originalImplementation,
signInUpPOST: async function (input) {
if (originalImplementation.signInUpPOST === undefined) { throw Error("Should never come here"); }
let response = await originalImplementation.signInUpPOST(input)
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" "github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels" "github.com/supertokens/supertokens-golang/supertokens")
func main() { supertokens.Init(supertokens.TypeInput{ RecipeList: []supertokens.Recipe{ thirdparty.Init(&tpmodels.TypeInput{ Override: &tpmodels.OverrideStruct{ APIs: func(originalImplementation tpmodels.APIInterface) tpmodels.APIInterface { // First we copy the original implementation originalSignInUpPOST := *originalImplementation.SignInUpPOST
(*originalImplementation.SignInUpPOST) = func(provider tpmodels.TypeProvider, code string, authCodeResponse interface{}, redirectURI string, options tpmodels.APIOptions, userContext supertokens.UserContext) (tpmodels.SignInUpPOSTResponse, error) { resp, err := originalSignInUpPOST(provider, code, authCodeResponse, redirectURI, options, userContext) if err != nil { return tpmodels.SignInUpPOSTResponse{}, err }
if resp.OK != nil { // the user logged in / signed up successfully
// In this example we are using Google as our provider authCodeResponse := resp.OK.AuthCodeResponse
accessToken := authCodeResponse.(map[string]interface{})["access_token"].(string)
fmt.Println(accessToken)
}
return resp, err }
return originalImplementation }, }, }), }, })}
from supertokens_python import init, InputAppInfofrom supertokens_python.recipe import thirdpartyfrom supertokens_python.recipe.thirdparty.interfaces import APIInterface, APIOptions, SignInUpPostOkResultfrom typing import Union, Dict, Anyfrom supertokens_python.recipe.thirdparty.provider import Provider
def override_thirdparty_apis(original_implementation: APIInterface): original_sign_in_up_post = original_implementation.sign_in_up_post async def 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: APIOptions, user_context: Dict[str, Any]): # First we call the original implementation of signInPOST. response = await original_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, SignInUpPostOkResult): _ = 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.sign_in_up_post = sign_in_up_post return original_implementation
init( app_info=InputAppInfo(api_domain="...", app_name="...", website_domain="..."), framework='...', recipe_list=[ thirdparty.init( override=thirdparty.InputOverrideConfig( apis=override_thirdparty_apis ), sign_in_and_up_feature=thirdparty.SignInAndUpFeature(providers=[ #... ]) ) ])