Last active
December 19, 2015 11:09
-
-
Save alairock/5945546 to your computer and use it in GitHub Desktop.
GeoCaching via CakePHP. Shows how to use connect via Oauth, get the token, and use the token to process a post request.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Place the following line above the class: | |
//App::import('Vendor', 'OAuth/OAuthClient'); | |
//OAuth component found here: http://code.42dh.com/oauth/ | |
public function index() { | |
$data = array( | |
'AccessToken' => $this->Session->read('access_token'), | |
'Usernames' => array('alairock'), | |
); | |
$request = array( | |
'header' => array( | |
'Content-Type' => 'application/json', | |
), | |
); | |
$data = json_encode($data); | |
$response = $this->HttpSocket->post('https://staging.api.groundspeak.com/Live/V6Beta/geocaching.svc/GetUsersCacheCounts?format=Json', $data, $request); | |
pr($response->body()); | |
exit; | |
} | |
/** | |
* Index method | |
*/ | |
public function getGeoKey() { | |
$this->HttpSocket = $this->__createClient(); | |
$requestToken = $this->HttpSocket->getRequestToken('https://staging.geocaching.com/OAuth/oauth.ashx', 'http://localhost/hubstats/hubs/callback'); | |
if ($requestToken) { | |
$this->Session->write('geocache_request_token', $requestToken); | |
$this->redirect('https://staging.geocaching.com/OAuth/oauth.ashx?oauth_token=' . $requestToken->key); | |
} else { | |
// an error occured when obtaining a request token | |
} | |
} | |
public function callback() { | |
$requestToken = $this->Session->read('geocache_request_token'); | |
$this->HttpSocket = $this->__createClient(); | |
$params = array( | |
'oauth_verifier' => $_GET['oauth_verifier'], | |
'oauth_token' => $_GET['oauth_token'], | |
); | |
$accessToken = $this->HttpSocket->getAccessToken('https://staging.geocaching.com/OAuth/oauth.ashx', $requestToken, 'POST', $params); | |
$this->Session->write('access_token', $accessToken->key); | |
$this->redirect(array('action' => 'index')); | |
} | |
private function __createClient() { | |
return new OAuthClient('Key', 'Secret'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment