Skip to content

Commit 211fb6b

Browse files
committed
init code commit
1 parent 35d698b commit 211fb6b

18 files changed

+7633
-2
lines changed

CODE_OF_CONDUCT.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, sex characteristics, gender identity and expression,
9+
level of experience, education, socio-economic status, nationality, personal
10+
appearance, race, religion, or sexual identity and orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at [email protected]. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72+
73+
[homepage]: https://www.contributor-covenant.org
74+
75+
For answers to common questions about this code of conduct, see
76+
https://www.contributor-covenant.org/faq

CONTRIBUTING.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Contributing to xero-php-oauth2-app
2+
:+1::tada: First off, thanks for taking the time to contribute! :tada::+1:
3+
4+
We want to make contributing to this project as easy and transparent as possible, whether it's:
5+
6+
- Reporting a bug
7+
- Discussing the current state of the code
8+
- Submitting a fix
9+
- Proposing new features
10+
- Becoming a maintainer
11+
12+
The following is a set of guidelines for contributing to Xero and its projects, which are hosted in the [XeroAPI Organization](https://github.com/XeroAPI) on GitHub. These are mostly guidelines, not rules. Use your best judgment, and feel free to propose changes to this document in a pull request.
13+
14+
### Table Of Contents
15+
[Code of Conduct](#code-of-conduct)
16+
17+
[How Can I Contribute?](#how-can-i-contribute)
18+
* [Reporting Bugs](#reporting-bugs)
19+
* [Suggesting Enhancements](#suggesting-enhancements)
20+
* [Pull Requests](#pull-requests)
21+
22+
23+
## Code of Conduct
24+
Please note that this project is released with a [Contributor Code of Conduct](https://github.com/XeroAPI/xero-php-oauth2-app/blob/master/CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
25+
26+
## I don't want to read this whole thing I just have a question!!!
27+
28+
> **Note:** Please don't file an issue to ask a question. You'll get faster results by using the resources below.
29+
30+
We have an official [message board](https://community.xero.com/developer) where Xero API support and the community chimes in with helpful advice if you have questions.
31+
32+
* [API Documentation](https://developer.xero.com/documentation/)
33+
* [Xero FAQ](https://developer.xero.com/faq)
34+
* [Community, the official Xero message board](https://community.xero.com/developer)
35+
* [Stackoverflow - Xero related](https://stackoverflow.com/questions/tagged/xero-api?sort=Newest)
36+
* [Submit requests for support](https://developer.xero.com/contact-xero-developer-platform-support/) to our API Support team.
37+
38+
# How Can I Contribute?
39+
40+
You can contribute by reporting bugs, suggesting enhancements, improving documentation i.e. [README](https://github.com/XeroAPI/xero-php-oauth2-app/blob/master/README.md) or submitting a pull request.
41+
42+
## Reporting Bugs
43+
44+
Bugs are tracked as issues in this repository.
45+
46+
Before opening a new issue:
47+
* **check [the README](https://github.com/XeroAPI/xero-php-oauth2-app/blob/master/README.md)** to see if the behavior you observed might be expected and if configuration options are available to provide you with the desired behavior.
48+
49+
* **perform a cursory search** to see if there's [an existing issue](https://github.com/XeroAPI/xero-php-oauth2-app/issues) covering your feedback. If there is one and the issue is still open, **add a :+1: reaction** on the issue to express interest in the issue being resolved. That will help the team gauge interest without the noise of comments which trigger notifications to all watchers. Comments should be used only if you have new and useful information to share.
50+
51+
### Write bug reports with detail, background, and sample code
52+
53+
- A quick summary and/or background
54+
- Steps to reproduce
55+
- Be specific!
56+
- Give sample code if you can.
57+
- What you expected would happen
58+
- What actually happens
59+
- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work)
60+
61+
## Suggesting Enhancements
62+
63+
When opening an issue for a feature request:
64+
* **use a clear and descriptive title** for the issue to identify the problem.
65+
* **include as many details as possible in the body**. Explain your use-case, the problems you're hitting and the solutions you'd like to see to address those problems.
66+
67+
## Pull requests
68+
69+
We welcome your feedback and pull requests. Be aware some code is generated and pull requests may not be the best solution - see below.
70+
71+
### Submit a pull request
72+
73+
1. Fork and clone the repository
74+
1. Create a new branch: `git checkout -b my-branch-name`
75+
1. Make your change, add tests if needed
76+
1. Make sure the tests pass on your machine: `mvn test`
77+
1. Push to your fork and submit a pull request
78+
1. Pat your self on the back and wait for your pull request to be reviewed and merged.
79+
80+
Here are a few things you can do that will increase the likelihood of your pull request being accepted:
81+
82+
- Write tests.
83+
- Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests.
84+
- Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html).
85+
86+
## License
87+
By contributing, you agree that your contributions will be licensed under its MIT License.

README.md

100644100755
Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,79 @@
1-
# xero-php-oauth2-app
2-
PHP app for demonstrating the xero-php-oauth2 SDK
1+
# Xero PHP oAuth 2 App
2+
This java project demonstrates how to use the Xero-Java SDK. Clone this repository to your local machine to begin.
3+
4+
This is a project is for use with xero-php-oauth2 SDK. This app demonstrates the functionality of Xero accounting API endpoints and their related actions.
5+
6+
You'll be able to connect to a Xero Organisation, make real API calls. The code used to make each API call will be displayed along with the results returned from Xero's API.
7+
8+
## Getting Started
9+
To run locally, you'll need a local web server with PHP support.
10+
* MAMP is a good option [Download MAMP](https://www.mamp.info/en/downloads/)
11+
12+
Follow these steps
13+
* Clone this repo into your webroot. Look in your MAMP folder for `htdocs`
14+
* Launch a terminal app and change to the newly cloned folder `xero-php-oauth2-app`
15+
* Download dependencies with Composer.
16+
17+
`composer init`
18+
19+
New to Composer? You'll want to [install Composer](https://getcomposer.org/doc/00-intro.md)
20+
21+
## Create a Xero App
22+
To obtain your API keys, follow these steps and create a Xero app
23+
24+
* Create a [free Xero user account](https://www.xero.com/us/signup/api/) (if you don't have one)
25+
* Login to [Xero developer center](https://developer.xero.com/myapps)
26+
* Click "Try oAuth2" link
27+
* Enter your App name, company url, privacy policy url.
28+
* Enter the redirect URI (this is your callback url - localhost, etc)
29+
* Agree to terms and condition and click "Create App".
30+
* Click "Generate a secret" button.
31+
* Copy your client id and client secret and save for use later.
32+
* Click the "Save" button. You secret is now hidden.
33+
34+
## Add your API keys to this app
35+
You'll need to set the *clientId, clientSecret and redirectUri* in the following files
36+
37+
* authorization.php
38+
* callback.php
39+
* get.php.
40+
41+
```php
42+
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
43+
'clientId' => '__YOUR_CLIENT_ID__',
44+
'clientSecret' => '__YOUR_CLIENT_SECRET__',
45+
'redirectUri' => 'http://localhost:8888/xero-php-oauth2-app/callback.php',
46+
'urlAuthorize' => 'https://login.xero.com/identity/connect/authorize',
47+
'urlAccessToken' => 'https://identity.xero.com/connect/token',
48+
'urlResourceOwnerDetails' => 'https://api.xero.com/api.xro/2.0/Organisation'
49+
]);
50+
```
51+
52+
## License
53+
54+
This software is published under the [MIT License](http://en.wikipedia.org/wiki/MIT_License).
55+
56+
Copyright (c) 2019 Xero Limited
57+
58+
Permission is hereby granted, free of charge, to any person
59+
obtaining a copy of this software and associated documentation
60+
files (the "Software"), to deal in the Software without
61+
restriction, including without limitation the rights to use,
62+
copy, modify, merge, publish, distribute, sublicense, and/or sell
63+
copies of the Software, and to permit persons to whom the
64+
Software is furnished to do so, subject to the following
65+
conditions:
66+
67+
The above copyright notice and this permission notice shall be
68+
included in all copies or substantial portions of the Software.
69+
70+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
71+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
72+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
73+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
74+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
75+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
76+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
77+
OTHER DEALINGS IN THE SOFTWARE.
78+
79+

authorization.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
ini_set('display_errors', 'On');
3+
require __DIR__ . '/vendor/autoload.php';
4+
require_once('storage.php');
5+
6+
// Storage Classe uses sessions for storing access token > extend to your DB of choice
7+
$storage = new StorageClass();
8+
9+
session_start();
10+
11+
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
12+
'clientId' => '__YOUR_CLIENT_ID__',
13+
'clientSecret' => '__YOUR_CLIENT_SECRET__',
14+
'redirectUri' => 'http://localhost:8888/xero-php-oauth2-app/callback.php',
15+
'urlAuthorize' => 'https://login.xero.com/identity/connect/authorize',
16+
'urlAccessToken' => 'https://identity.xero.com/connect/token',
17+
'urlResourceOwnerDetails' => 'https://api.xero.com/api.xro/2.0/Organisation'
18+
]);
19+
20+
// If we don't have an authorization code then get one
21+
if (!isset($_GET['code'])) {
22+
$options = [
23+
'scope' => ['openid email profile offline_access accounting.settings accounting.transactions accounting.contacts accounting.journals.read accounting.reports.read accounting.attachments']
24+
];
25+
26+
// Fetch the authorization URL from the provider; this returns the urlAuthorize option and generates and applies any necessary parameters (e.g. state).
27+
$authorizationUrl = $provider->getAuthorizationUrl($options);
28+
29+
// Get the state generated for you and store it to the session.
30+
$_SESSION['oauth2state'] = $provider->getState();
31+
32+
// Redirect the user to the authorization URL.
33+
header('Location: ' . $authorizationUrl);
34+
exit();
35+
36+
// Check given state against previously stored one to mitigate CSRF attack
37+
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
38+
unset($_SESSION['oauth2state']);
39+
exit('Invalid state');
40+
} else {
41+
42+
}
43+
?>
44+
<html>
45+
<head>
46+
<title>My App</title>
47+
</head>
48+
<body>
49+
Opps! Problem redirecting .....
50+
</body>
51+
</html>

callback.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
ini_set('display_errors', 'On');
3+
require __DIR__ . '/vendor/autoload.php';
4+
require_once('storage.php');
5+
6+
// Storage Classe uses sessions for storing token > extend to your DB of choice
7+
$storage = new StorageClass();
8+
9+
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
10+
'clientId' => '__YOUR_CLIENT_ID__',
11+
'clientSecret' => '__YOUR_CLIENT_SECRET__',
12+
'redirectUri' => 'http://localhost:8888/xero-php-oauth2-app/callback.php',
13+
'urlAuthorize' => 'https://login.xero.com/identity/connect/authorize',
14+
'urlAccessToken' => 'https://identity.xero.com/connect/token',
15+
'urlResourceOwnerDetails' => 'https://api.xero.com/api.xro/2.0/Organisation'
16+
]);
17+
18+
// If we don't have an authorization code then get one
19+
if (!isset($_GET['code'])) {
20+
echo "NO CODE";
21+
header("Location: index.php?error=true");
22+
exit();
23+
24+
// Check given state against previously stored one to mitigate CSRF attack
25+
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
26+
echo "Invalid State";
27+
unset($_SESSION['oauth2state']);
28+
exit('Invalid state');
29+
} else {
30+
try {
31+
// Try to get an access token using the authorization code grant.
32+
$accessToken = $provider->getAccessToken('authorization_code', [
33+
'code' => $_GET['code']
34+
]);
35+
36+
$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken( (string)$accessToken->getToken() );
37+
38+
$config->setHost("https://api.xero.com");
39+
$identityInstance = new XeroAPI\XeroPHP\Api\IdentityApi(
40+
new GuzzleHttp\Client(),
41+
$config
42+
);
43+
44+
// Get Array of Tenant Ids
45+
$result = $identityInstance->getConnections();
46+
47+
// Save my token, expiration and tenant_id
48+
$storage->setToken(
49+
$accessToken->getToken(),
50+
$accessToken->getExpires(),
51+
$result[0]->getTenantId(),
52+
$accessToken->getRefreshToken());
53+
54+
header('Location: ' . './get.php');
55+
exit();
56+
57+
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
58+
echo "Failed!!!";
59+
// Failed to get the access token or user details.
60+
exit($e->getMessage());
61+
}
62+
}
63+
?>
64+
<html>
65+
<head>
66+
<title>My App</title>
67+
</head>
68+
<body>
69+
Opps! Should have redirected to <a href="get.php">to this page</a>
70+
</body>
71+
</html>

0 commit comments

Comments
 (0)