public function authorize() { $oauth_handler = new \Evernote\Auth\OauthHandler($this->sandbox, false, $this->china); try { $oauth_data = $oauth_handler->authorize($this->key, $this->secret, $this->getCallbackUrl()); $this->token = $oauth_data['oauth_token']; $ret = $this->token; } catch (\Evernote\Exception\AuthorizationDeniedException $e) { //If the user decline the authorization, an exception is thrown. $ret = null; } return $ret; }
<?php require __DIR__ . '/../../vendor/autoload.php'; /** Understanding SANDBOX vs PRODUCTION Environments * * The Evernote API 'Sandbox' environment -> SANDBOX.EVERNOTE.COM * - Create a sample Evernote account at https://sandbox.evernote.com * * The Evernote API 'Production' Environment -> WWW.EVERNOTE.COM * - Activate your Sandboxed API key for production access at https://dev.evernote.com/support/ * * For testing, set $sandbox to true, for production, set $sandbox to false * */ $sandbox = true; $oauth_handler = new \Evernote\Auth\OauthHandler($sandbox); $key = '%key%'; $secret = '%secret%'; $callback = 'http://host/pathto/evernote-cloud-sdk-php/sample/oauth/index.php'; try { $oauth_data = $oauth_handler->authorize($key, $secret, $callback); echo "\nOauth Token : " . $oauth_data['oauth_token']; // Now you can use this token to call the api $client = new \Evernote\Client($oauth_data['oauth_token']); } catch (Evernote\Exception\AuthorizationDeniedException $e) { //If the user decline the authorization, an exception is thrown. echo "Declined"; }
/** * yx和ev第三方授权登陆,授权同步 evernote控制器 * @param bool $china yx还是ev * @param bool $granted 是不是包括授权操作 */ public function evernote() { require_once APP_DIR . "vendors/auth/yx/vendor/autoload.php"; $key = 'ripenaloe-2993'; $secret = 'd6901d0dd5cb3b8c'; $sandbox = false; $china = (bool) $this->input->get('china'); //认证之后是否是授权备份 $granted = (bool) $this->input->get('granted'); //认证之后是否是绑定账户 $auth_action = $this->input->get('do'); $oauth_handler = new \Evernote\Auth\OauthHandler($sandbox, false, $china); // 就是当前域名的当前方法 $callback = '//' . $_SERVER['HTTP_HOST'] . '?c=' . str_replace('Controller\\', '', __CLASS__) . '&a=' . __FUNCTION__ . '&china=' . $china . '&granted=' . $granted . '&do=' . $auth_action; try { $oauth_data = $oauth_handler->authorize($key, $secret, $callback); if (!$oauth_data) { return false; } $oauth_token = $oauth_data['oauth_token']; $edam_userId = $oauth_data['edam_userId']; $edam_expires = $oauth_data['edam_expires']; // secret为空,不需要 // $oauth_token_secret = $oauth_data['oauth_token_secret']; // shard对我们目前没用,不保存 // $edam_shard = $oauth_data['edam_shard']; // 同理 // $edam_noteStoreUrl = $oauth_data['edam_noteStoreUrl']; // 同理 // $edam_webApiUrlPrefix = $oauth_data['edam_webApiUrlPrefix']; $client = new \Evernote\Client($oauth_token, false, null, null, $china); // 查看是否是备份操作 if ($granted) { $this->evernoteBackup($oauth_token, $edam_userId, $edam_expires, $china); } else { if ($auth_action == 'bind') { //绑定账户 $this->evernoteBind($client, $oauth_token, $edam_userId, $edam_expires, $china); } else { $uid = $this->evernoteSignin($client, $oauth_token, $edam_userId, $edam_expires, $china); if ($uid) { // 转移数据 $this->transferData($uid); // 登录 $this->passport->login($uid, 'password'); } } } } catch (Exception $e) { } // 关闭窗口 $this->output->view('passport/evernote'); }
} return '<br />Evernote API Test'; })->bind('api/evernote/test'); /** * * Evernote auth * */ $app->get('/api/evernote/auth', function (Request $request) use($app) { $token = $app['session']->get('api/evernote/token', false); $reset = $request->query->get('reset', false); if (!$reset && $token) { return $app->redirect($app->url('api/evernote')); } $sandbox = true; $handler = new \Evernote\Auth\OauthHandler($sandbox, false, false); $key = $app['env']['app']['evernote']['key']; $secret = $app['env']['app']['evernote']['secret']; $callback = $app->url('api/evernote/auth'); try { $oauth = $handler->authorize($key, $secret, $callback); $headers = headers_list(); foreach ($headers as $h) { if (strpos($h, 'evernote.com/OAuth.action') !== false) { exit; } } if (!$oauth || !isset($oauth['oauth_token'])) { throw new Exception('Auth failed'); } $app['session']->set('api/evernote/token', $oauth['oauth_token']);