Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

api - Problem with "refresh Token" in STRAVA and database

I don't understand how to update the token with the refresh token. I work in PHP and use the class "Iamstuartwilson" (https://packagist.org/packages/iamstuartwilson/strava)

Achievement in the index.php page that the user authorizes with Strava, I save the user's data in a database. When the token expires, I can't get it to update automatically with the refresh token.

initial login page (index.php)

<?php 
include "funcion/conexion.php";
require_once 'vendor/iamstuartwilson/strava/src/iamstuartwilson/StravaApi.php';


$clientId = '58400';
$clientSecret = '2b7f169336f1dea8bc1404452a5d297eb5bcb867';
$redirect = 'http://localhost/prueba2/index.php';

$api = new IamstuartwilsonStravaApi($clientId, $clientSecret);

if (!isset($_GET['code'])) {
    print '<a href="'.$api->authenticationUrl($redirect, $approvalPrompt = 'auto', $scope = 'read,profile:read_all,activity:read,activity:read_all', $state = null).'">Conectar con strava</a>';
}else{
    $code = $_GET['code'];
    $result = $api->tokenExchange($code);

    $nombre = $result->athlete->firstname.'&nbsp;'.$result->athlete->lastname;
    $ciudad =  $result->athlete->city;
    $sexo =  $result->athlete->sex;
    $strava = $result->athlete->id;
    $perfil = $result->athlete->profile_medium;
    $accessToken = $result->access_token;
    $refreshToken = $result->refresh_token;
    $expiresAt = $result->expires_at;


$getid = $db->query("SELECT * FROM ciclistas WHERE nombre = '$nombre'");
$resid = $getid->fetch(PDO::FETCH_OBJ);
if ($resid == null) {
$resultado = 1;
$consulta = '';
}else{
    $resultado = $resid->idciclista;
    $consulta = $resid->strava;
}

if ($consulta != $strava) {
    $sql = $db->prepare("INSERT INTO ciclistas(nombre,ciudad,strava,sexo,perfil,access_token,code,refresh_token,expires_at) VALUES (?,?,?,?,?,?,?,?,?)");
    $res = $sql->execute([$nombre,$ciudad,$strava,$sexo,$perfil,$accessToken,$code,$refreshToken,$expiresAt]);

$getid = $db->query("SELECT * FROM ciclistas WHERE nombre = '$nombre'");
$resid = $getid->fetch(PDO::FETCH_OBJ);
$resultado = $resid->idciclista;

}
}

 ?>

It works ok since it generates access token, refresh token and expires. I store user data with their tokens in a database

In another page test.php, I want to see the activities of the users to save them in another database. I can do it until the access token expires.-

Here is the test.php page

<?php 
include "funcion/conexion.php";
require_once 'vendor/iamstuartwilson/strava/src/iamstuartwilson/StravaApi.php';

$time = time();
$clientId = '58400';
$clientSecret = '2b7f169336f1dea8bc1404452a5d297eb5bcb867';
$redirect = 'http://localhost/prueba2/index.php';

$api = new IamstuartwilsonStravaApi($clientId, $clientSecret);

$gettoken =  $db->query("SELECT * FROM ciclistas");
$restoken = $gettoken->fetch(PDO::FETCH_OBJ);

$existingAccessToken = $restoken->access_token; // $token del código anterior
$expires = $restoken->expires_at;
$refreshtoken = $restoken->refresh_token;

  echo "<pre>";
  print_r($restoken);     
  echo "</pre>";

 echo "<pre>";
  print_r($expires);echo "<br>";
  print_r($time);     
  echo "</pre>";

if ($expires < $time){
    
    $new = $api->tokenExchangeRefresh($refreshtoken);
        

echo "<pre>";
    print_r($api);
    echo "</pre>";

}

When the token lifetime expires, it does not automatically renew me with the refresh token. Something will know that I do wrong in the catch or logic or where to make it work?

Thanks a lot

question from:https://stackoverflow.com/questions/65843233/problem-with-refresh-token-in-strava-and-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...