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
- Other Frameworks
Important
For other backend frameworks, you can follow our guide on how to spin up a separate server configured with the SuperTokens backend SDK to authenticate requests and issue session tokens.
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.oAuthTokens["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, input tpmodels.TypeSignInUpInput, tenantId string, options tpmodels.APIOptions, userContext supertokens.UserContext) (tpepmodels.ThirdPartySignInUpPOSTResponse, error) {
// first we call the original implementation
resp, err := originalThirdPartySignInUpPOST(provider, input, tenantId, options, userContext)
if err != nil {
return tpepmodels.ThirdPartySignInUpPOSTResponse{}, err
}
// if sign in / up was successful...
if resp.OK != nil {
authCodeResponse := resp.OK.OAuthTokens
accessToken := authCodeResponse["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, Optional
from supertokens_python.recipe.thirdparty.provider import Provider, RedirectUriInfo
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,
redirect_uri_info: Optional[RedirectUriInfo],
oauth_tokens: Optional[Dict[str, Any]],
tenant_id: str,
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, redirect_uri_info, oauth_tokens, tenant_id, 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.oauth_tokens
access_token = thirdparty_auth_response["access_token"]
print(access_token)
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
)
)
]
)