Skip to content

Commit b79f082

Browse files
Stephen SmithStephen Smith
authored andcommitted
Merge remote-tracking branch 'xerooauth/master'
2 parents 16e843c + d0c2845 commit b79f082

File tree

14 files changed

+6325
-0
lines changed

14 files changed

+6325
-0
lines changed

xerooauth/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.settings
2+
.project
3+
.buildpath
4+
.gitattributes
5+
*.pem
6+
*.p12
7+
*.cer
8+
*.pfx
9+
<<<<<<< HEAD
10+
.*
11+
=======
12+
>>>>>>> 65378c19b093593e1019e5cf6b6ffc72e389ecc5

xerooauth/README.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
XeroOAuth-PHP
2+
-----------------------
3+
4+
PHP library for working with the Xero OAuth API.
5+
6+
Intro
7+
======
8+
XeroOAuth-PHP is a sample library for use with the Xero API (<http://developer.xero.com>). The Xero API uses OAuth 1.0a, but we would not recommend using this library for other OAuth 1.0a APIs as
9+
the Xero API has one of the more advanced implementations (RSA-SHA1, client ssl certs etc) and thus has many configuration options not typically used in other APIs.
10+
11+
This library is designed to get a developer up and running quickly with the OAuth authentication layer, but there will be some customisation of its implementation required before it can be used in a production environment.
12+
13+
## Requirements
14+
* PHP 5+
15+
* php\_curl extension - ensure a recent version (7.30+)
16+
* php\_openssl extension
17+
18+
19+
## Setup
20+
To get setup, you will need to modify the values in the \_config.php file to your own requirements and application settings or see the customised example file for each different application type, public.php, private.php or partner.php.
21+
_Special options for Partner applications_ should be commented out for non-partner applications if using the \_config.php file.
22+
23+
## Usage
24+
25+
There are a number of functions used when interacting with Xero:
26+
27+
#### Make a request
28+
The request function lies at the core of any communication with the API. There are a number of types of requests you may wish to make, all handled by the request() function.
29+
30+
request($method, $url, $parameters, $xml, $format)
31+
32+
###### Parameters
33+
* Method: the API method to be used (GET, PUT, POST)
34+
* URL: the URL of the API endpoint. This is handled by a special function (see below)
35+
* Parameters: an associative array of parameters such as where, order by etc (see <http://developer.xero.com/documentation/getting-started/http-requests-and-responses/>)
36+
* XML: request data (for PUT and POST operations)
37+
* Format: response format (currently xml, json & pdf are supported). Note that PDF is not supported for all endpoints
38+
39+
#### Generate a URL
40+
Create a properly formatted request URL.
41+
42+
url($endpoint, $api)
43+
44+
###### Parameters
45+
* Endpoint: the endpoint you wish to work with. Note there are OAuth endpoints such as 'RequestToken' and 'AccessToken' in addition to various API endpoints such as Invoices, Contacts etc. When specifying a resource, such as Invoices/$GUID, you can construct the request by appending the GUID to the base URL.
46+
* API: there are two APIs: core (core accounting API) and payroll (payroll application API). Default is core.
47+
48+
#### Parse the response
49+
Once you get data back, you can pass it through the parseResponse function to turn it into something usable.
50+
51+
parseResponse($response, $format)
52+
53+
###### Parameters
54+
* Response: the raw API response to be parsed
55+
* Format: xml pdf and json are supported, but you cannot use this function to parse an XML API response as JSON - must correspond to the requested response format.
56+
57+
#### Authorise
58+
For public and partner API type applications using the 3-legged OAuth process, we need to redirect the user to Xero to authorise the API connection. To do so, redirect the user to a url generated with a call like this:
59+
60+
url("Authorize", '') . "?oauth_token=".$oauth_token."&scope=" . $scope;
61+
62+
###### Appendages
63+
* oauth\_token: this is a request token generated in a prior RequestToken call
64+
* scope: the Payroll API is a permissioned API and required a comma separated list of endpoints the application is requesting access to e.g. $scope = 'payroll.payrollcalendars,payroll.superfunds,payroll.payruns,payroll.payslip,payroll.employees';
65+
66+
67+
#### Refresh an access token
68+
For partner API applications where the 30 minute access tokens can be programatically refreshed via the API, you can use the refreshToken function:
69+
70+
refreshToken('the access token', 'the session handle')
71+
72+
###### Parameters
73+
* Access token: the current access token
74+
* Session handle: the session identifier handle
75+
76+
## Debug
77+
78+
###### Setup Diagnostics
79+
As you are getting set up, you may run into a few configuration issues, particularly with some of the more advanced application types such as partner.
80+
81+
To make sure your configuration is correct, you can run a diagnostics function:
82+
83+
diagnostics();
84+
85+
This returns an array of error messages (if there are any). These are in human readable form so should be enough to put you on the right track. If not, check the Xero developer centre and forum for more detail.
86+
87+
It would probably be a bad idea to run this in your production code as the errors returned ones only a developer can resolve, not the end user.
88+
89+
###### Runtime errors
90+
91+
There are many reasons why an error may be encountered: data validation, token issues, authorisation revocation etc. It is important to inspect not just the HTTP response code, but also the associated error string.
92+
93+
A very basic error output function is included in the sample code, which outputs all available information related to an error. It would need to be substantially tidied up before the results could be surfaced in a production environment.
94+
95+
outputError($object);
96+
97+
98+
## Response Helpers
99+
Understanding the type of message you are getting from the API could be useful. In each response that is not successful, a helper element is returned:
100+
101+
* **TokenExpired:** This means that the access token has expired. If you are using a partner API type application, you can renew it automatically, or if using a public application, prompt the user to re-authenticate
102+
* **TokenFatal:** In this scenario, a token is in a state that it cannot be renewed, and the user will need to re-authenticate
103+
* **SetupIssue:** There is an issue within the setup/configuration of the connection - check the diagnostics function
104+
105+
## TODO
106+
107+
- [ ] Reading a value from a report
108+
- [x] Better WHERE and ORDER examples
109+
- [ ] Merge OAuthsimple changes for RSA-SHA1 back to parent repo
110+
111+
112+
## License & Credits
113+
114+
This software is published under the [MIT License](http://en.wikipedia.org/wiki/MIT_License).
115+
116+
###### OAuthSimple
117+
OAuthsimple.php contains minor adaptations from the OAuthSimple PHP class by [United Heroes](http://unitedheroes.net/OAuthSimple/).
118+
119+
###### tmhOAuth
120+
XeroOAuth class is based on code and structure derived from the [tmhOAuth](https://github.com/themattharris/tmhOAuth) library.
121+
122+
## Major change history
123+
124+
#### 0.6 - 19th April 2015
125+
126+
Added composer support.
127+
Modified content-type so is also set for PUT requests
128+
129+
#### 0.5 - 16th November 2014
130+
131+
Added examples for CRU of tracking categories and options.
132+
Updated the CA certs to a recent one - warning that if you are using a very old version of curl you may get 'cert invalid' type error.
133+
Removed an unused function and tidied up comments on another to make them more sensible.
134+
135+
#### 0.4 - 29th September 2014
136+
137+
Merged some pull requests, addressed an issue with multiple calls having signature validation issues.
138+
139+
#### 0.3 - 3rd January 2014
140+
141+
Merged a number of pull requests, tidied up formatting and extended sample tests.
142+
143+
#### 0.2 - 13th May 2013
144+
145+
Merged to master, added more tests and improved security handling for partner API apps.
146+
147+
148+
#### 0.1 - 10th May 2013
149+
150+
Initial release candidate prepared and released to 'refactor' branch.

xerooauth/_config.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* @file
4+
* A single location to store configuration.
5+
*/
6+
7+
/**
8+
* Define for file includes. The certs directory is best stored out of web root so moving the directory
9+
* and updating the reference to BASE_PATH is the best way to ensure things keep working
10+
*/
11+
define('BASE_PATH',dirname(__FILE__));
12+
13+
/**
14+
* Define which app type you are using:
15+
* Private - private app method
16+
* Public - standard public app method
17+
* Partner - partner app method
18+
*/
19+
define("XRO_APP_TYPE", "Partner");
20+
21+
/**
22+
* Set a user agent string that matches your application name as set in the Xero developer centre
23+
*/
24+
$useragent = "";
25+
26+
/**
27+
* Set your callback url or set 'oob' if none required
28+
*/
29+
define("OAUTH_CALLBACK", 'http://localhost/XeroOAuth-PHP/example.php');
30+
31+
/**
32+
* Application specific settings
33+
* Not all are required for given application types
34+
* consumer_key: required for all applications
35+
* consumer_secret: for partner applications, set to: s (cannot be blank)
36+
* rsa_private_key: application certificate private key - not needed for public applications
37+
* rsa_public_key: application certificate public cert - not needed for public applications
38+
*/
39+
40+
$signatures = array(
41+
'consumer_key' => 'MWSAN8S5AAFPMMNBV3DQIEWH4TM9FE',
42+
'shared_secret' => 's',
43+
// API versions
44+
'core_version'=> '2.0',
45+
'payroll_version'=> '1.0',
46+
'file_version' => '1.0'
47+
);
48+
49+
if (XRO_APP_TYPE=="Private"||XRO_APP_TYPE=="Partner") {
50+
$signatures['rsa_private_key']= BASE_PATH . '/certs/privatekey.pem';
51+
$signatures['rsa_public_key']= BASE_PATH . '/certs/publickey.cer';
52+
}
53+
54+
55+
/**
56+
* Special options for Partner applications
57+
* Partner applications require a Client SSL certificate which is issued by Xero
58+
* the certificate is issued as a .p12 cert which you will then need to split into a cert and private key:
59+
* openssl pkcs12 -in entrust-client.p12 -clcerts -nokeys -out entrust-cert.pem
60+
* openssl pkcs12 -in entrust-client.p12 -nocerts -out entrust-private.pem <- you will be prompted to enter a password
61+
*/
62+
if (XRO_APP_TYPE=="Partner") {
63+
$signatures['curl_ssl_cert'] = BASE_PATH . '/certs/entrust-cert-RQ3.pem';
64+
$signatures['curl_ssl_password'] = '1234';
65+
$signatures['curl_ssl_key'] = BASE_PATH . '/certs/entrust-private-RQ3.pem';
66+
}
67+
68+
69+

0 commit comments

Comments
 (0)