Issues with refresh tokens when authenticating with multiple users concurrently
AnsweredI am proactively moving over some code to using tokens rather than user credentials, as per the recent email. I feel a bit derpy as I can't seem to figure out how to get it to do what I need! We need to be able to check that our clients haven't accidentally removed our maintenance account from their workspaces that we use for automating various user tasks. The following code achieves that on the first run. We will usually iterate through this several times with different clients to make sure they are all working. We are using refresh tokens each time, which we update and store in the database. However, when we re-iterate through a client again (one that has already been run), it gives the error at the bottom. Currently this is in testing, so I can manually invalidate the token in the database and force a re-authentication through another page. Any ideas why this would be the case?
Connect our script to Podio using our client_id
Podio::setup( PODIO_CLIENT_ID, PODIO_CLIENT_SECRET );
Authenticate the user credentials with Podio
Podio::authenticate( 'refresh_token', array( 'refresh_token' => $user_auth_code, 'redirect_uri' => PODIO_REDIRECT_URI ) );
Get the refresh token that was returned
$refresh_token = sanitize( Podio::$oauth->refresh_token );
Update the database with the new code
$pb_db->query( "UPDATE tblUser SET auth_code = '$refresh_token' WHERE userId = $user_id" );
Get the details for this user
$user_status = PodioUserStatus::get();
Iterate through the organisations
foreach ( $organisations as $organisation_id ) {
## Get a list of all of the workspaces for this organisation
$spaces_for_org = PodioSpace::get_for_org( $organisation_id );
## Iterate through the spaces
foreach ( $spaces_for_org as $space ) {
## Skip this space if the user is not an admin
if ( PodioSpaceMember::get( $space->space_id, $user_status->profile->user_id )->role != 'admin' ) continue;
## Get the current role for the Admin user
$space_role = PodioSpaceMember::get( $space->space_id, PODIO_ADMIN_ID )->role;
## If the user isn't an admin on the space, add them
if ( $space_role != 'admin' ) PodioSpaceMember::add( $space->space_id, array( 'role' => 'admin', 'users' => array( PODIO_ADMIN_ID ) ) );
}
}
Fatal error: Uncaught PodioInvalidGrantError: "Invalid refresh_token" Request URL: http://api.podio.com/oauth/token Stack Trace: #0 /var/www/html//podio/lib/Podio.php(95): Podio::request('POST', '/oauth/token', Array, Array) #1 /var/www/html/modules/maintain.php(156): Podio::authenticate('refresh_token', Array) #2 {main} thrown in /var/www/html/podio/lib/Podio.php on line 291
Please sign in to leave a comment.
Comments
1 comment