定制 ultra-nick/peloton-auth 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ultra-nick/peloton-auth

最新稳定版本:v1.0.1

Composer 安装命令:

composer require ultra-nick/peloton-auth

包简介

PHP class for automated Peloton OAuth token management - no authorised endpoint required.

README 文档

README

A PHP class for automated Peloton API OAuth token management. Handles the full authentication lifecycle - login, token refresh, and credential fallback - without requiring an authorised Peloton API endpoint.

Disclaimer: This is an unofficial library with no affiliation with Peloton Interactive, Inc. It uses Peloton's web OAuth PKCE flow, which may change at any time. Use at your own risk and in accordance with Peloton's terms of service.

Features

  • Obtains Peloton API OAuth access and refresh tokens from username and password alone
  • Automatically refreshes tokens before they expire
  • Falls back to full credential login if the refresh token has expired
  • Once set up, requires no manual intervention
  • No config files - credentials and tokens are passed directly, keeping storage in your control

Requirements

  • PHP 7.4 or higher
  • cURL extension enabled

Installation

Via Composer

composer require ultra-nick/peloton-auth

Manual

Copy PelotonAuth.php into your project and require_once it directly.

Usage

Basic usage

Instantiate the class with your Peloton credentials and call getTokenData(). That's it - the library handles everything else internally.

require_once 'PelotonAuth.php';

$auth = new PelotonAuth('your@email.com', 'yourpassword');
$tokens = $auth->getTokenData();

echo $tokens->access_token;
echo $tokens->refresh_token;
echo $tokens->user_id;
echo $tokens->expires_at; // Unix timestamp

Persisting tokens between runs

The library manages tokens in memory. To avoid a full login on every run, store the tokens yourself and pass them back in on the next instantiation:

// Load previously stored tokens (from a file, database, etc.)
$stored = json_decode(file_get_contents('tokens.json'));

$auth = new PelotonAuth(
    'your@email.com',
    'yourpassword',
    $stored->access_token  ?? null,
    $stored->refresh_token ?? null
);

$tokens = $auth->getTokenData();

// Save the updated tokens for next time
file_put_contents('tokens.json', json_encode($tokens));

Making API requests

getTokenData() returns the tokens you need to make your own API requests:

$tokens = $auth->getTokenData();

$ch = curl_init('https://api.onepeloton.com/api/me');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => [
        'Authorization: Bearer ' . $tokens->access_token,
        'Content-Type: application/json',
    ],
]);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);  // Deprecated from PHP 8.5 - safe to remove if running PHP 8.0+

How it works

Peloton's API uses Auth0 for authentication. This library implements the OAuth 2.0 PKCE (Proof Key for Code Exchange) flow - the same flow used by the Peloton web app.

Token lifecycle is managed automatically in three tiers:

  1. Valid token - returned immediately with no network call
  2. Token expired, refresh token present - silently refreshes using the stored refresh token
  3. No tokens, or refresh fails - performs a full PKCE login using the stored credentials

API Reference

Constructor

new PelotonAuth(
    string  $username,
    string  $password,
    ?string $accessToken  = null,
    ?string $refreshToken = null
)
Parameter Type Description
$username string Peloton account email
$password string Peloton account password
$accessToken string|null Previously stored access token (optional)
$refreshToken string|null Previously stored refresh token (optional)

getTokenData(): object

Returns a valid token data object, refreshing or re-authenticating as needed.

$tokens = $auth->getTokenData();

Returns an object with the following properties:

Property Type Description
access_token string Bearer token for Peloton API requests
refresh_token string|null Token used to obtain a new access token
expires_at int|null Token expiry as a Unix timestamp
user_id string|null Peloton user ID decoded from the JWT

Throws \RuntimeException if all authentication attempts fail.

Security

  • Store your credentials and token files outside your web root
  • Never commit credentials or token files to version control - add them to .gitignore
  • Treat access and refresh tokens with the same care as passwords

Contributing

Issues and pull requests are welcome. If Peloton's API auth flow changes and this library breaks, please open an issue.

License

MIT - see LICENSE for details.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固