문제

I am having this weird issue here in which the instant I get a token via Dropbox and try to test it, an exception is thrown with this message:

HTTP status 401 {"error": "The given OAuth 2 access token doesn't exist or has expired."}

Why would a Dropbox token expire as soon as I generate it?

Let me show you the flow of my app:

User sees this screen:

Form 1

Which has the following relevant PHP code:

require "../../includes/dropbox-sdk/Dropbox/autoload.php";
require "../../includes/config.php";
session_start();
session_regenerate_id();
$appInfo = Dropbox\AppInfo::loadFromJsonFile("../../includes/dropbox-sdk/Dropbox/app-info.json");
$webAuth = new Dropbox\WebAuthNoRedirect($appInfo, "Mignori-Box/1.0");

$authorizeUrl = $webAuth->start();

So okay, we generate a Dropbox authorization URL with the official SDK, which seems to be working fine, the user sees this screen:

Dropbox

They click allow, Dropbox gives them an Authorization token, then they paste it into my site, and submit it. This is the code that processes the form:

      try
      {
          $accessToken = $_POST['authorization_code'];

          $dbxClient = new Dropbox\Client($accessToken, "Mignori-Box/1.0");
          $accountInfo = $dbxClient->getAccountInfo();
      }catch(Exception $e)
      {
          echo "<div class=\"alert alert-danger\">";
          echo "<strong>An error has occurred.</strong><br>" . $e -> getMessage();
          echo "</div>";
      }

But no matter how recent the token is, when the user submits the token the catch is called and the warning displayed. Even if the token was generated literally seconds ago, this happens.

Can someone please enlighten me as to what's wrong?

도움이 되었습니까?

해결책

I was forgetting the step to convert the authorization code into an access token.

list($accessToken, $dropboxUserId) = $webAuth->finish($authCode);

In other words, I was indeed using the wrong token. Haha. Last snippet of code should be:

 try
  {
      list($accessToken, $dropboxUserId) = $webAuth->finish($_POST['authorization_code']);

      $dbxClient = new Dropbox\Client($accessToken, "Mignori-Box/1.0");
      $accountInfo = $dbxClient->getAccountInfo();
  }catch(Exception $e)
  {
      echo "<div class=\"alert alert-danger\">";
      echo "<strong>An error has occurred.</strong><br>" . $e -> getMessage();
      echo "</div>";
  }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top