Php 如何转换Box';s";代码“;至;代币;用户允许访问后
我是新手,尝试连接Box的API v2。我成功地建立了一个PHP客户端库,这要归功于developers.box.com/auth上第一段中的链接。我已经完整阅读了Box的演练两次以上,这里有大约100000个关于这个问题的问题和回答。我的问题发生在用户重定向到Box的授权页面,输入他的凭据并单击“允许”之后。结果根据我的重定向uri和我的登录页面的url而有所不同,我在其中放置了我的客户端id和客户端机密:1)如果我的重定向uri与我的匹配,用户显然会重定向到同一url,然后将用户发送回Box的授权页面;2)如果我的重定向uri与页面不同,则用户成功返回到我的重定向uri,其url包含30秒代码。我知道我很快就能弄明白这一点,但不知道如何在30秒或更短的时间内将代码转换为令牌,并使用它显示用户的文件夹、文件、信息或其他任何内容。非常感谢您的考虑。我的立场是:Php 如何转换Box';s";代码“;至;代币;用户允许访问后,php,authentication,oauth-2.0,access-token,box-api,Php,Authentication,Oauth 2.0,Access Token,Box Api,我是新手,尝试连接Box的API v2。我成功地建立了一个PHP客户端库,这要归功于developers.box.com/auth上第一段中的链接。我已经完整阅读了Box的演练两次以上,这里有大约100000个关于这个问题的问题和回答。我的问题发生在用户重定向到Box的授权页面,输入他的凭据并单击“允许”之后。结果根据我的重定向uri和我的登录页面的url而有所不同,我在其中放置了我的客户端id和客户端机密:1)如果我的重定向uri与我的匹配,用户显然会重定向到同一url,然后将用户发送回Box
// mysite.com/client.php:
// ...
case 'Box':
$this->oauth_version = '2.0';
$this->request_token_url = '';
$this->dialog_url = 'https://api.box.com/oauth2/authorize?client_id={CLIENT_ID}&response_type=code&redirect_uri={REDIRECT_URI}&state={STATE}';
$this->append_state_to_redirect_uri = '';
$this->access_token_url = 'https://api.box.com/oauth2/token';
$this->authorization_header = true;
$this->url_parameters = false;
break;
// ...
看起来您需要重定向URL与最初通过OAuth进程向用户发送的URL不同
例如,您可以使用
https://mysite.com/login_with_box
通过OAuth进程发送用户,然后https://mysite.com/receive_box_oauth_response
是在身份验证过程之后重定向到的URL,并处理来自box的OAuth响应。我找到了它。这个问题当然完全是我的错。下面是我如何将Box API v2与Box推荐的PHP OAuth库连接起来的:
case 'Box':
$this->oauth_version = '2.0';
$this->request_token_url = '';
$this->dialog_url = 'https://api.box.com/oauth2/authorize?response_type=code&client_id={CLIENT_ID}&state={STATE}';
$this->append_state_to_redirect_uri = '';
$this->access_token_url = 'https://api.box.com/oauth2/token';
$this->authorization_header = true;
$this->url_parameters = false;
break;
<?php
require('http.php');
require('oauth_client.php');
$client = new oauth_client_class;
$client->server = 'Box';
$client->redirect_uri = 'https://mysite.com/oauth/login_with_box.php';
$client->client_id = 'xxxxxx_BOX_API_CLIENT_ID_xxxxxx';
$client->client_secret = 'xxxxxx_BOX_API_CLIENT_SECRET_xxxxxx';
if(strlen($client->client_id) == 0 || strlen($client->client_secret) == 0)
die('You need an app to do that.');
if(($success = $client->Initialize())) {
if(($success = $client->Process())) {
if(strlen($client->access_token)) {
$success = $client->CallAPI(
'https://api.box.com/2.0/folders/0',
'GET', array('format'=>'json'), array('FailOnAccessError'=>true), $folder);
}
}
$success = $client->Finalize($success);
}
if($client->exit)
exit;
if($success) {
?>
<!doctype html>
<html>
<head>
<title>Box OAuth client results</title>
</head>
<body>
<?php echo '<h1>You successfully logged in with Box</h1>'; echo '<pre>', HtmlSpecialChars(print_r($folder, 1)), '</pre>'; ?>
</body>
</html>
<?php } else { ?>
<!doctype html>
<html>
<head>
<title>OAuth client error</title>
</head>
<body>
<h1>OAuth client error</h1>
<pre>Error: <?php echo HtmlSpecialChars($client->error); ?></pre>
</body>
</html>
<?php } ?>
框OAuth客户端结果
OAuth客户端错误
OAuth客户端错误
错误:
我希望这能帮助一些人。谢谢肖恩,事实上,我的重定向URL与我的问题有很大关系。我很快就会发布我的解决方案。嗨,我只是想知道(如果你还在的话)你会如何使用这种方法添加和删除文件,我可以使用这种方法来获得连接,但我不确定从哪里开始:\Hi A_Wheel_Monkey,一旦你有了访问令牌,您可以在任何其他地方包括PHP OAuth库,并为任何其他地方调用API。例如,前面的代码为我的重定向uri mysite.com/oauth/login_和_box.php提供了支持。允许我成功访问其邮箱的用户返回mysite.com/oauth/login_with_Box.php,在那里她看到一条“Success!”消息和指向mysite.com/oauth/files.php的链接。Files.php包含库,但不是调用API获取访问令牌,而是请求其他任何内容,例如API.box.com/2.0/Files/content。我意识到我当时没有收集访问令牌,所以我需要继续向API发送访问令牌的请求,我正在编辑oauth类,以便使用刷新令牌获取新的访问令牌(它无法处理),我就快到了,所以如果您想查看我拥有的内容,我将与您共享(oauth类很大,因此我将尽我所能尝试引用)听起来不错,您签出$rev了吗?博克斯付钱给自由职业者?
<?php
require('http.php');
require('oauth_client.php');
$client = new oauth_client_class;
$client->server = 'Box';
$client->redirect_uri = 'https://mysite.com/oauth/login_with_box.php';
$client->client_id = 'xxxxxx_BOX_API_CLIENT_ID_xxxxxx';
$client->client_secret = 'xxxxxx_BOX_API_CLIENT_SECRET_xxxxxx';
if(strlen($client->client_id) == 0 || strlen($client->client_secret) == 0)
die('You need an app to do that.');
if(($success = $client->Initialize())) {
if(($success = $client->Process())) {
if(strlen($client->access_token)) {
$success = $client->CallAPI(
'https://api.box.com/2.0/folders/0',
'GET', array('format'=>'json'), array('FailOnAccessError'=>true), $folder);
}
}
$success = $client->Finalize($success);
}
if($client->exit)
exit;
if($success) {
?>
<!doctype html>
<html>
<head>
<title>Box OAuth client results</title>
</head>
<body>
<?php echo '<h1>You successfully logged in with Box</h1>'; echo '<pre>', HtmlSpecialChars(print_r($folder, 1)), '</pre>'; ?>
</body>
</html>
<?php } else { ?>
<!doctype html>
<html>
<head>
<title>OAuth client error</title>
</head>
<body>
<h1>OAuth client error</h1>
<pre>Error: <?php echo HtmlSpecialChars($client->error); ?></pre>
</body>
</html>
<?php } ?>