{"_id":"556785670145bc23008e3bb8","__v":1,"initVersion":null,"project":"556785630145bc23008e3bb3","tags":[],"user":{"_id":"55678558820d1a3700874dd4","username":"","name":"Milenko Beslic"},"createdAt":"2015-05-28T21:15:19.734Z","changelog":[{"_id":"5577059107e7110d00104367","update":"link to sign up","type":"improved"},{"_id":"5577059107e7110d00104366","update":"","type":"added"}],"body":"[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"PHP Example of Authentication\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass pingup\\n    {\\n        const liveEndpoint = \\\"https://api.pingup.com/\\\";\\n        const sandboxEndpoint = \\\"http://api.sandbox.pingup.com/\\\";\\n        const apiVersion = \\\"v1\\\";\\n        \\n        private $token = null;\\n        private $endpoint = null;\\n         \\n        public function __construct($authToken, $sandbox = false)\\n        {\\n            if (isset($authToken))\\n            {\\n                $this->token = $authToken;\\n                \\n                if ($sandbox)\\n                    $this->endpoint = pingup::sandboxEndpoint;\\n                else $this->endpoint = pingup::liveEndpoint;\\n            }\\n            else throw new Exception(\\\"Pingup: No access token provided.\\\");\\n        }\\n         \\n        public static function generateTokens($key, $secret, $sandbox = false)\\n        {\\n            return pingup::authRequest(\\\"POST\\\", http_build_query(Array(\\\"key\\\" => $key, \\\"secret\\\" => $secret)), \\\"auth/generateTokens\\\", $sandbox);\\n        }\\n        \\n        public static function refreshToken($refreshToken, $secret, $sandbox = false)\\n        {\\n            return pingup::authRequest(\\\"PUT\\\", http_build_query(Array(\\\"token\\\" => $refreshToken, \\\"secret\\\" => $secret)), \\\"auth/refreshToken\\\", $sandbox);\\n        }\\n        \\n        public static function authRequest($method, $parameters, $path, $sandbox)\\n        {\\n            // Format endpoint\\n            if ($sandbox)\\n                $endpoint = \\\"http://api.sandbox.pingup.com/\\\";\\n            else $endpoint = \\\"https://api.pingup.com/\\\";\\n            \\n            $path .= \\\"?$parameters\\\";          \\n            // Make the request\\n            $array = json_decode(shell_exec(\\\"curl -L -X $method -H \\\\\\\"Content-type: application/json\\\\\\\" \\\\\\\"\\\" . $endpoint . $path . \\\"\\\\\\\"\\\"), true);\\n            \\n            // Was request successful?\\n            if (isset($array[\\\"httpStatus\\\"]))\\n                throw new Exception(\\\"Pingup API error \\\" . $array[\\\"responseReference\\\"] . \\\": \\\" . $array[\\\"message\\\"] . \\\" \\\" . $array[\\\"description\\\"]);\\n            else return $array;\\n        }\\n    }\\n?>\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"node.js Example of Authentication\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"//// Using Superagent for easy REST calls ////\\nvar request = require('superagent');\\nvar strToken = \\\"my generated token goes here\\\";\\n\\n//// POST with key and secret in query string to /generateTokens  ////\\nfunction retrieveToken(callback) {\\nrequest\\n    .post('http://api.sandbox.pingup.com:80/auth/generateTokens?key=abc&secret=123')\\n   .end(function(err, res){\\n    strToken = res.body.token.accessToken;\\n\\tcallback()\\n  });\\n};\\n\\n//// GET with token in header to /places  ////\\nfunction getPlaces() {\\n\\tconsole.log(\\\"token:\\\" + strToken);\\n\\trequest\\n\\t   .get('http://api.sandbox.pingup.com:80/v1/places?postCode=02130&name=Green%20Street%20Massage&street=183%20Green')\\n\\t   .set('token', strToken)\\n\\t   .end(function(err, res){\\n       console.log(res.body);\\n\\t  });\\n}\\n\\nretrieveToken(getPlaces);\\n\",\n      \"language\": \"javascript\",\n      \"name\": \"node.js\"\n    }\n  ]\n}\n[/block]","slug":"welcome-to-pingup","title":"Authentication Examples"}

Authentication Examples


[block:api-header] { "type": "basic", "title": "PHP Example of Authentication" } [/block] [block:code] { "codes": [ { "code": "<?php\n\nclass pingup\n {\n const liveEndpoint = \"https://api.pingup.com/\";\n const sandboxEndpoint = \"http://api.sandbox.pingup.com/\";\n const apiVersion = \"v1\";\n \n private $token = null;\n private $endpoint = null;\n \n public function __construct($authToken, $sandbox = false)\n {\n if (isset($authToken))\n {\n $this->token = $authToken;\n \n if ($sandbox)\n $this->endpoint = pingup::sandboxEndpoint;\n else $this->endpoint = pingup::liveEndpoint;\n }\n else throw new Exception(\"Pingup: No access token provided.\");\n }\n \n public static function generateTokens($key, $secret, $sandbox = false)\n {\n return pingup::authRequest(\"POST\", http_build_query(Array(\"key\" => $key, \"secret\" => $secret)), \"auth/generateTokens\", $sandbox);\n }\n \n public static function refreshToken($refreshToken, $secret, $sandbox = false)\n {\n return pingup::authRequest(\"PUT\", http_build_query(Array(\"token\" => $refreshToken, \"secret\" => $secret)), \"auth/refreshToken\", $sandbox);\n }\n \n public static function authRequest($method, $parameters, $path, $sandbox)\n {\n // Format endpoint\n if ($sandbox)\n $endpoint = \"http://api.sandbox.pingup.com/\";\n else $endpoint = \"https://api.pingup.com/\";\n \n $path .= \"?$parameters\"; \n // Make the request\n $array = json_decode(shell_exec(\"curl -L -X $method -H \\\"Content-type: application/json\\\" \\\"\" . $endpoint . $path . \"\\\"\"), true);\n \n // Was request successful?\n if (isset($array[\"httpStatus\"]))\n throw new Exception(\"Pingup API error \" . $array[\"responseReference\"] . \": \" . $array[\"message\"] . \" \" . $array[\"description\"]);\n else return $array;\n }\n }\n?>", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "node.js Example of Authentication" } [/block] [block:code] { "codes": [ { "code": "//// Using Superagent for easy REST calls ////\nvar request = require('superagent');\nvar strToken = \"my generated token goes here\";\n\n//// POST with key and secret in query string to /generateTokens ////\nfunction retrieveToken(callback) {\nrequest\n .post('http://api.sandbox.pingup.com:80/auth/generateTokens?key=abc&secret=123')\n .end(function(err, res){\n strToken = res.body.token.accessToken;\n\tcallback()\n });\n};\n\n//// GET with token in header to /places ////\nfunction getPlaces() {\n\tconsole.log(\"token:\" + strToken);\n\trequest\n\t .get('http://api.sandbox.pingup.com:80/v1/places?postCode=02130&name=Green%20Street%20Massage&street=183%20Green')\n\t .set('token', strToken)\n\t .end(function(err, res){\n console.log(res.body);\n\t });\n}\n\nretrieveToken(getPlaces);\n", "language": "javascript", "name": "node.js" } ] } [/block]
improved
link to sign up