PHP:Dropbox Token已过期或不存在

时间:2015-01-29 06:33:00

标签: php dropbox-api

我正在尝试将文件从localhost复制到Dropbox帐户,我正在使用curl生成令牌,一旦我获得令牌,我正在使用它来上传文件但是我收到错误说令牌已过期或不存在。

这是我的代码:

## step1.php ##

  <?php
    session_start();

    require_once('config.php');

    $step_2_url = rawurlencode("http://nuftp.com/sanjeev/step-2.php");

   $ch = curl_init(); 

     $headers = array( 'Authorization: OAuth oauth_version="1.0",                     
  oauth_signature_method="PLAINTEXT", oauth_consumer_key="' . $app_key . '",  
  oauth_signature="' . $app_secret . '&"' );

  curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); 
   curl_setopt( $ch, CURLOPT_URL,  
  "https://api.dropbox.com/1/oauth/request_token" );  
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );  
     $request_token_response = curl_exec( $ch );
   error_log(var_export($request_token_response,true));


   parse_str( $request_token_response, $parsed_request_token );

   $json_access = json_decode( $request_token_response );

  if ( isset( $json_access->error ) ) {
    echo '<br><br>FATAL ERROR: ' . $json_access->error . '<br><br>';
    die();
    }

   $_SESSION['myapp'] = array();
   $_SESSION['myapp']['oauth_request_token'] =       
    $parsed_request_token['oauth_token'];
   $_SESSION['myapp']['oauth_request_token_secret'] = 
  $parsed_request_token['oauth_token_secret'];

  header( 'Location: https://www.dropbox.com/1/oauth/authorize?oauth_token=' 
. $parsed_request_token['oauth_token'] . '&oauth_callback=' . $step_2_url );

## step2.php ##

       <?php

       session_start();

      require_once('config.php');

     include 'dropbox-sdk-php-1.1.4/lib/Dropbox/autoload.php';

   use \Dropbox as dbx;


  $step_3_url = "step-3.php";
     if ( isset( $_GET['oauth_token'] ) && isset( $_GET['uid'] ) && isset(   
     $_SESSION['myapp'] ) ) {

  $ch = curl_init(); 

  $headers = array( 'Authorization: OAuth oauth_version="1.0",  

    oauth_signature_method="PLAINTEXT", oauth_consumer_key="' . $app_key .  
   '", oauth_token="'  .$_GET['oauth_token'] . '", oauth_signature="' .   
  $app_secret . '&' . $_SESSION['myapp']['oauth_request_token_secret'] . '"'  
  );
   curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); 

  curl_setopt( $ch, CURLOPT_URL, 
  "https://api.dropbox.com/1/oauth/access_token" );  
   curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );  
    $access_token_response = curl_exec( $ch );

    error_log(var_export($request_token_response,true));

     parse_str( $access_token_response, $parsed_access_token );

    error_log( $access_token_response );

     $json_access = json_decode( $access_token_response );



   if ( isset( $json_access->error ) ) {
    echo '<br><br>FATAL ERROR: ' . $json_access->error . '<br><br>';
    die();
   }

   $_SESSION['myapp']['uid'] = $parsed_access_token['uid'];
   $_SESSION['myapp']['oauth_access_token'] =      
    $parsed_access_token['oauth_token'];
    $_SESSION['myapp']['oauth_access_token_secret'] = 
    $parsed_access_token['oauth_token_secret'];



   $dbxClient = new dbx\Client($_GET['oauth_token'], "PHP-Example/1.0");
   $accountInfo = $dbxClient->getAccountInfo();
     print_r($accountInfo);

      $f = fopen("working-draft.txt", "rb");
     $result = $dbxClient->uploadFile("/working-draft.txt",  
   dbx\WriteMode::add(), $f);
   fclose($f);
   print_r($result);

   $folderMetadata = $dbxClient->getMetadataWithChildren("/");
    print_r($folderMetadata);


    $f = fopen("working-draft.txt", "w+b");
    $fileMetadata = $dbxClient->getFile("/working-draft.txt", $f);
    fclose($f);
     print_r($fileMetadata);


    }

有人请建议我做什么以及我哪里出错。

1 个答案:

答案 0 :(得分:0)

我设法得到另一个工作代码,它的工作非常好。它在dropbox上执行文件的上传和下载。您可以从下面的链接获取工作代码,但是在代码中我手动更改要上载的文件的名称,而不是将其保存在变量中,或者提供从目录中选择文件的选项。 如果有任何疑问,请在此处发表评论,并将其还原至sanjeevks101@gmail.com 在实际测试代码之前,请先阅读说明页面。

http://www.filehosting.org/file/details/471563/AccessDropboxUsingPHP-master.rar