Reputation: 7004
I am setting up Stripe Connect as explained here (Standalone Account). I handle the authorization and the retrieval of the access_token
on my node server.
The user can visit the link MY_SERVER_URI/authorize
and will be redirected to a pre-defined stripe AUTHORIZE_URI
:
app.get("/authorize", function(req, res) {
// Redirect to Stripe /oauth/authorize endpoint
res.redirect(AUTHORIZE_URI + "?" + qs.stringify({
response_type: "code",
scope: "read_write",
client_id: CLIENT_ID
}));
});
After the user authorizes Stripe Connect, he or she will be redirected to a pre-defined REDIRECT_URI, which in this case equals to MY_SERVER_URI/oauth/callback
, where the following script is executed:
app.get("/oauth/callback", function(req, res) {
var code = req.query.code;
// Make /oauth/token endpoint POST request
request.post({
url: TOKEN_URI,
form: {
grant_type: "authorization_code",
client_id: CLIENT_ID,
code: code,
client_secret: API_KEY
}
}, function(err, r, body) {
var accessToken = JSON.parse(body).access_token;
// Do something with your accessToken
// For demo"s sake, output in response:
res.send({ "Your Token": accessToken });
});
});
Now everything here works fine and the application is able to get the accessToken
. However, this accessToken
needs to be saved and matched with the user who is granting the access from the client side.
My question therefore boils down to, how can I either pass a client-side parameter (like the client-side userId) in the oauth/callback
GET
request, or process the server handling on the client side (e.g. a $http
GET
request instead of visiting the uri)? I guess that the later is not the recommended option.
I made two attempts:
REDIRECT_URI
, but the
problem is that Stripe requires that alle urls need to be specified
first (resulting that no parameters can be passed in the redirect
url).MY_STRIPE_URI/authorize
with a $http
GET
request, but this gave me the obvious error No 'Access-Control-Allow-Origin' header is present on the requested resourceWhat can be done?
Upvotes: 1
Views: 2999
Reputation: 111
You have to pass your user id as "state" parameter and Stripe will return it on the callback. The only way I found to avoid session
Upvotes: 11
Reputation: 15488
Generally your scenario is as follows:
req.session.user = {id: '...'}
req.session.user.access_token = '...'
Note:
authorization_code
OAuth flowUpvotes: 1