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 ThirdPartyEmailPassword from "supertokens-node/recipe/thirdpartyemailpassword";
import Session from "supertokens-node/recipe/session";
SuperTokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
supertokens: {
connectionURI: "...",
},
recipeList: [
ThirdPartyEmailPassword.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/thirdpartyemailpassword"
"github.com/supertokens/supertokens-golang/recipe/thirdpartyemailpassword/tpepmodels"
"github.com/supertokens/supertokens-golang/supertokens"
)
func main() {
supertokens.Init(supertokens.TypeInput{
RecipeList: []supertokens.Recipe{
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
Override: &tpepmodels.OverrideStruct{
APIs: func(originalImplementation tpepmodels.APIInterface) tpepmodels.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) (tpepmodels.ThirdPartyOutput, error) {
// first we call the original implementation
resp, err := originalThirdPartySignInUpPOST(provider, code, authCodeResponse, redirectURI, options, userContext)
if err != nil {
return tpepmodels.ThirdPartyOutput{}, 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, InputAppInfo
from supertokens_python.recipe import thirdpartyemailpassword
from supertokens_python.recipe.thirdpartyemailpassword.interfaces import APIInterface, ThirdPartyAPIOptions, ThirdPartySignInUpPostOkResult
from typing import Dict, Any, Union
from 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=[
thirdpartyemailpassword.init(
override=thirdpartyemailpassword.InputOverrideConfig(
apis=apis_override
)
)
]
)