PHP:Dropbox 令牌已过期或不存在
PHP: Dropbox Token Expired or Does not exist
我正在尝试将一个文件从本地主机复制到 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);
}
有人请告诉我该怎么做以及我哪里出错了。
我设法获得了另一个工作代码,它工作得非常好。它在保管箱上执行文件的上传和下载。您可以从下面的 link 中获取工作代码,但是在代码中我手动更改要上传的文件的名称,而不是将其保存在变量中或提供从目录中选择文件的选项。
如果有任何疑问,请在此处发表评论,并在 sanjeevks101@gmail.com 上回复我
在实际测试代码之前,还要先浏览说明页面。
http://www.filehosting.org/file/details/471563/AccessDropboxUsingPHP-master.rar
我正在尝试将一个文件从本地主机复制到 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);
}
有人请告诉我该怎么做以及我哪里出错了。
我设法获得了另一个工作代码,它工作得非常好。它在保管箱上执行文件的上传和下载。您可以从下面的 link 中获取工作代码,但是在代码中我手动更改要上传的文件的名称,而不是将其保存在变量中或提供从目录中选择文件的选项。 如果有任何疑问,请在此处发表评论,并在 sanjeevks101@gmail.com 上回复我 在实际测试代码之前,还要先浏览说明页面。
http://www.filehosting.org/file/details/471563/AccessDropboxUsingPHP-master.rar