我正在添加一个事件侦听器,然后尝试将其删除,如下所示: 场景一 在上面的场景中,我的监听器被移除了,但是当我将一些值绑定为时它并没有被移除: 场景 2 为什么事件侦听器在 Scenario-1 而不是在 Scenario-2 中被删除? 我如何绑定像 Scenario-2 这样的值,以便 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我在CakePHP 2.5.6中使用了Guzzle 3.8.1,并且正在对Box API进行调用。 我希望能够在访问令牌过期时刷新访问令牌。 我遇到的问题是,我可能正在使用N个可能的访问令牌中的任何一个,并且希望能够使用要在其中定义该功能的范围内的$ account_id。我看不到如何将其传递给Listener定义的函数,因为我自己没有调用它。 我确实看到$ event变量中存在过期的access_token,但我必须找到它,然后从我传递给Box的标头中对其进行解析。 我想我可以添加一个包含account_id的自定义标头,但这似乎是解决该问题的一种坏方法。 我可以以某种方式在请求中指定一个不会作为Box调用的一部分添加的变量吗? 任何建议都非常感谢,谢谢!
`
public function get_folder_list ($account_id = null, $parent_folder_id = null) {
// Get account
$this->Account = ClassRegistry::init('Account');
$account = $this->Account->findById($account_id);
$this->Account->log($account);
// If account doesn't exist, throw error
if(empty($account)) {
throw new NotFoundException(__('Account id not found: ' . $account_id));
}
// Call Box for folder list
$box_url = "https://api.box.com/2.0/folders/";
if (empty($parent_folder_id)) {
$box_url .= "0";
} else {
$box_url .= $parent_folder_id;
}
$this->Account->log($box_url);
$bearer = 'Bearer ' . $account['Account']['access_token'];
$client = new Guzzle\Http\Client();
$client->getEventDispatcher()->addListener('request.error', function(Event $event) {
if ($event['response']->getStatusCode() == 401) {
$Account = new Account;
$Account->log($event);
$Box = new BoxLib;
$account = $Box->refresh_token($account_id);
$bearer = 'Bearer ' . $account['Account']['access_token'];
$request = $client->get($box_url, array('Authorization' => $bearer));
$event['response'] = $newResponse;
$event->stopPropagation();
}
});
$request = $client->get($box_url, array('Authorization' => $bearer));
try {
$response = $request->send();
} catch (Guzzle\Http\Exception\BadResponseException $e) {
// HTTP codes 4xx and 5xx throw BadResponseException
$this->Account->log('Service exception!');
$this->Account->log('Uh oh! ' . $e->getMessage());
$this->Account->log('HTTP request URL: ' . $e->getRequest()->getUrl() . "\n");
$this->Account->log('HTTP request: ' . $e->getRequest() . "\n");
$this->Account->log('HTTP response status: ' . $e->getResponse()->getStatusCode() . "\n");
$this->Account->log('HTTP response: ' . $e->getResponse() . "\n");
}
$this->Account->log($response->json());
$json = $response->json();
$folder_list = array();
foreach ($json['item_collection']['entries'] as $folder) {
$folder_list[$folder['id']] = $folder['name'];
}
//$this->Account->log($folder_list);
return $folder_list;
}
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.