dvicklund/ebay-oauth-php-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dvicklund/ebay-oauth-php-client

Composer 安装命令:

composer require dvicklund/ebay-oauth-php-client

包简介

Enables the simple retreival of an eBay API OAuth token

README 文档

README

Code coverage badge

Allows developers to fetch an OAuth token that can be used to call the eBay Developer REST APIs using PHP.

What is OAuth

OAuth 2.0 is the most widely used standard for authentication and authorization for API based access. The complete end to end documentation on how eBay OAuth functions may be used is available at developer.ebay.com. See: https://developer.ebay.com/api-docs/static/oauth-tokens.html

Installation

Include in your project via composer:

$ composer require dvicklund/ebay-oauth-php-client

Usage

EbayAuthToken(config)

Create a new instance of EbayAuthToken with a relevant config.

use EbayOauthToken\EbayOauthToken;

$ebayAuthToken = new EbayOauthToken([
    'env' => '<SANDBOX|PRODUCTION>', // If not provided, env defaults to PRODUCTION
    'clientId' => '<your_client_id>',
    'clientSecret' => '<your_client_secret>',
    'redirectUri' => '<redirect uri>'
]);
$ebayAuthToken->getApplicationToken(environment)

Generate client credential token.

$token = $ebayAuthToken->getApplicationToken('PRODUCTION');
print($token);
$ebayAuthToken->generateUserAuthorizationUrl(environment, scopes[, options])

Generate user consent authorization url.

$authUrl = $ebayAuthToken->generateUserAuthorizationUrl('PRODUCTION', $scopes);
print($authUrl);

You can also provide optional values:
state: An opaque value used by the client to maintain state between the request and callback.
prompt: Force a user to log in when you redirect them to the Grant Application Access page, even if they already have an existing user session.

The method call above could also be done as

$options = [ 'state' => 'custom-state-value', 'prompt' => 'login' ];
$authUrl = $ebayAuthToken->generateUserAuthorizationUrl('PRODUCTION', $scopes, $options);
print($authUrl);
$ebayAuthToken->exchangeCodeForAccessToken(environment, code)

Get a User access token.

$accessToken = $ebayAuthToken->exchangeCodeForAccessToken('PRODUCTION', $code);
print($accessToken);
$ebayAuthToken->getAccessToken(environment, refreshToken, scopes)

Use a refresh token to update a User access token (Updating the expired access token).

$accessToken = $ebayAuthToken->getAccessToken('PRODUCTION', $refreshToken, $scopes);
print($accessToken);

Library Setup and getting started

  1. Invoke the oauth ebay library as given below
use EbayOauthToken\EbayOauthToken;

$ebayAuthToken = new EbayOauthToken([
    'filePath' => 'demo/eBayJson.json' // input file path.
]);

OR

$ebayAuthToken = new EbayOauthToken([
    'clientId' => '<your_client_id>',
    'clientSecret' => '<your_client_secret>',
    'redirectUri' => '<redirect_uri_name>'
]);
  1. If you want to get your application credentials such as AppId, DevId, and CertId. Refer to Creating eBay Developer Account for details on how to get these credentials.
  2. You can refer to Example.php for an example of how to use credentials.
  3. For Authorization code grant
    1. Get user consent url using $ebayAuthToken->generateUserAuthorizationUrl()
    2. Open the generateUserAuthorizationUrl in the browser, which allows you to login in to ebay site. You will get an authorization code.
    3. Pass the authorization code retrieved in the above step to exchangeCodeForAccessToken method using $ebayAuthToken->exchangeCodeForAccessToken($environment, $code)

Configure credentials

Create a config JSON file in your application. The config file should contain your eBay applications keys: App Id, Cert Id & Dev Id. A sample config file is available at demo/ebay-config-sample.json. You could also set these parameters in a .env file, and pass them into the $options object during setup.

{
    "SANDBOX": {
        "clientId": "---Client Id---",
        "clientSecret": "--- client secret---",
        "devid": "-- dev id ---",
        "redirectUri": "-- redirect uri ---",
        "baseUrl": "api.sandbox.ebay.com" //don't change these values
    },
    "PRODUCTION": {
        "clientId": "---Client Id---",
        "clientSecret": "--- client secret---",
        "devid": "-- dev id ---",
        "redirectUri": "-- redirect uri ---",
        "baseUrl": "api.ebay.com" //don't change these values
    }
}

Types of Tokens

There are two types of tokens you will need to use.

Application Token

An application token contains an application identity which is generated using client_credentials grant type. These application tokens are useful for interaction with application specific APIs such as usage statistics, etc.

User Token

A user token (access token or refresh token) contains a user identity and the application’s identity. This is usually generated using the authorization_code grant type or the refresh_token grant type.

Supported Grant Types for OAuth

All of the regular OAuth 2.0 specifications such as client_credentials, authorization_code, and refresh_token are supported. Refer to eBay Developer Portal

Client Credentials

This grant type can be performed by simply using $ebayAuthToken->getApplicationToken(). Read more about this grant type at oauth-client-credentials-grant.

Authorization Code

This grant type can be performed by a two step process. Call $ebayAuthToken->generateUserAuthorizationUrl($environment, $scopes, $state) to get the Authorization URL to redirect the user to. Once the user authenticates and approves the consent, the callback needs to be captured by the redirect URL setup by the app and then call $ebayAuthToken->exchangeCodeForAccessToken($environment, $code) to get the refresh and access tokens.

Read more about this grant type at oauth-authorization-code-grant.

Refresh Token

This grant type can be performed by simply using $ebayAuthToken->getAccessToken($environment, $refreshToken, $scopes). Usually, access tokens are short lived. If the access token is expired, the caller can use the refresh token to generate a new access token. Read more about using a refresh token to update a user access token

Questions/problems?

If you have found a bug/issue, please file it on GitHub.

References

  1. https://developer.ebay.com/api-docs/static/oauth-tokens.html

  2. https://developer.ebay.com/api-docs/static/oauth-quick-ref-user-tokens.html

  3. https://developer.ebay.com/api-docs/static/oauth-gen-app-token.html

  4. https://developer.ebay.com/my/keys

License

Copyright (c) 2023 David Vicklund.

Use of this source code is governed by an Apache-2.0 license that can be found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0.

Useful links

dvicklund/ebay-oauth-php-client 适用场景与选型建议

dvicklund/ebay-oauth-php-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.44k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 dvicklund/ebay-oauth-php-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dvicklund/ebay-oauth-php-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 dvicklund/ebay-oauth-php-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2023-06-12