Coder Social home page Coder Social logo

oauth2-yahoo's Introduction

Yahoo Provider for OAuth 2.0 Client

Build Status Coverage Status Scrutinizer Code Quality

This package provides Yahoo OAuth 2.0 support for the PHP League's OAuth 2.0 Client.

This package is compliant with PSR-1, PSR-2 and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Requirements

The following versions of PHP are supported.

  • PHP 5.6
  • PHP 7.0
  • PHP 7.1
  • HHVM

Installation

To install, use composer:

composer require hayageek/oauth2-yahoo

Usage

Authorization Code Flow

session_start();
require('vendor/autoload.php');

$provider = new Hayageek\OAuth2\Client\Provider\Yahoo([
    'clientId'     => '{Yahoo-app-id}',
    'clientSecret' => '{Yahoo-app-secret}',
    'redirectUri'  => 'https://example.com/callback-url',
]);

if (!empty($_GET['error'])) {

    // Got an error, probably user denied access
    exit('Got error: ' . $_GET['error']);

} elseif (empty($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    // If we want to set approve page language (default is 'en-us')
    // $authUrl = $provider->getAuthorizationUrl(['language' => 'zh-tw']);
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: ' . $authUrl);
    exit;

} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    // State is invalid, possible CSRF attack in progress
    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

    // Optional: Now you have a token you can look up a users profile data
    try {

        // We got an access token, let's now get the owner details
        $ownerDetails = $provider->getResourceOwner($token);

        //Use these details to create a new profile
        echo 'Name: '.$ownerDetails->getName()."<br>";
	    echo 'FirstName: '.$ownerDetails->getFirstName()."<br>";
    	echo 'Lastname: '.$ownerDetails->getLastName()."<br>";
    
	    echo 'Email: '.$ownerDetails->getEmail()."<br>";
	    echo 'Image: '.$ownerDetails->getAvatar()."<br>";    
        
        

    } catch (Exception $e) {

        // Failed to get user details
        exit('Something went wrong: ' . $e->getMessage());

    }

    
	// Use this to interact with an API on the users behalf
	echo "Token: ". $token->getToken()."<br>";

	// Use this to get a new access token if the old one expires
	echo  "Refresh Token: ".$token->getRefreshToken()."<br>";

	// Number of seconds until the access token will expire, and need refreshing
	echo "Expires:" .$token->getExpires()."<br>";

    
    
}

Refreshing a Token

$provider = new Hayageek\OAuth2\Client\Provider\Yahoo([
    'clientId'     => '{Yahoo-app-id}',
    'clientSecret' => '{Yahoo-app-secret}',
    'redirectUri'  => 'https://example.com/callback-url',
]);

$grant = new League\OAuth2\Client\Grant\RefreshToken();
$token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);

// Use this to interact with an API on the users behalf
echo "Token: ". $token->getToken()."<br>";

// Use this to get a new access token if the old one expires
echo  "Refresh Token: ".$token->getRefreshToken()."<br>";

// Number of seconds until the access token will expire, and need refreshing
echo "Expires:" .$token->getExpires()."<br>";

Testing

$ ./vendor/bin/phpunit

Contributing

Credits

License

The MIT License (MIT). Please see License File for more information.

oauth2-yahoo's People

Contributors

hayageek avatar hialan avatar timothyasp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

oauth2-yahoo's Issues

Invalid response received from Authorization Server. Expected JSON.

hi,
I'm create an yahoo app:
Oath Ad Platforms and OpenID Connect Permissions

The scope: i add 'admg-w','profile','email' (yahoo documentation).
the test is in localshost:8000

I launch the connexion i accept but afert il have this error:

Invalid response received from Authorization Server. Expected JSON.
$yahooUser = $client->fetchUserFromToken($accessToken);dump($yahooUser);

I use google/facebook/instagram without problem. could you help me pls.
Thk

Not installing in local system by composer.

After you have the issue no #4.
I am trying to install in my local several time through composer. But it not working. Because it requires OAuth-client 1.0 but I have oauth2-google 2.0.

Please help me.
selection_015

Documentation Request: using oob

This is a documentation request to include information about utilizing 'oob' as the callback; potentially useful for local development.

Can't install because it requires oauth-client 1.0 but I have 2.0

I believe the solution is to change the requirement in your package's composer.json field to allow for league/oauth2-client of version 2.0 or greater, but I can't test that.

Using version ^2.0 for hayageek/oauth2-yahoo
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: don't install hayageek/oauth2-yahoo 2.0.4
    - Conclusion: don't install hayageek/oauth2-yahoo 2.0.2
    - Conclusion: don't install hayageek/oauth2-yahoo 2.0.1
    - Conclusion: remove league/oauth2-client 2.2.1
    - Installation request for hayageek/oauth2-yahoo ^2.0 -> satisfiable by hayageek/oauth2-yahoo[2.0.0, 2.0.1, 2.0.2, 2.0.4].
    - Conclusion: don't install league/oauth2-client 2.2.1
    - hayageek/oauth2-yahoo 2.0.0 requires league/oauth2-client ^1.0@dev -> satisfiable by league/oauth2-client[1.0.0, 1.0.1, 1.0.2, 1.1.0, 1.2.0, 1.3.0, 1.4.0, 1.4.1, 1.4.2].
    - Can only install one of: league/oauth2-client[1.0.0, 2.2.1].
    - Can only install one of: league/oauth2-client[1.0.1, 2.2.1].
    - Can only install one of: league/oauth2-client[1.0.2, 2.2.1].
    - Can only install one of: league/oauth2-client[1.1.0, 2.2.1].
    - Can only install one of: league/oauth2-client[1.2.0, 2.2.1].
    - Can only install one of: league/oauth2-client[1.3.0, 2.2.1].
    - Can only install one of: league/oauth2-client[1.4.0, 2.2.1].
    - Can only install one of: league/oauth2-client[1.4.1, 2.2.1].
    - Can only install one of: league/oauth2-client[1.4.2, 2.2.1].
    - Installation request for league/oauth2-client (locked at 2.2.1) -> satisfiable by league/oauth2-client[2.2.1].


Installation failed, reverting ./composer.json to its original content.

Fatal error in trying to get the user's profile image

Hi, I updated to 2.0.5, and after I agree with Yahoo! to use my Yahoo! account, I am redirected back to my site, and then this error occurs:

PHP Fatal error:  Uncaught Error: Cannot use object of type GuzzleHttp\Psr7\Response as array in /website/vendor/hayageek/oauth2-yahoo/src/Provider/Yahoo.php:122
Stack trace:
#0 /website/vendor/hayageek/oauth2-yahoo/src/Provider/Yahoo.php(105): Hayageek\OAuth2\Client\Provider\Yahoo->getUserImageUrl(Array, Object(League\OAuth2\Client\Token\AccessToken))
#1 /website/vendor/league/oauth2-client/src/Provider/AbstractProvider.php(754): Hayageek\OAuth2\Client\Provider\Yahoo->createResourceOwner(Array, Object(League\OAuth2\Client\Token\AccessToken))
#2 /website/account.php(116): League\OAuth2\Client\Provider\AbstractProvider->getResourceOwner(Object(League\OAuth2\Client\Token\AccessToken))
#3 {main}
  thrown in /website/vendor/hayageek/oauth2-yahoo/src/Provider/Yahoo.php on line 122

In the meantime, since I don't need the image URL, I commented out lines 122-126 of vendor/hayageek/oauth2-yahoo/src/Provider/Yahoo.php.

/*
        if (isset($image['image']['imageUrl'])) {
            return $image['image']['imageUrl'];
        }
*/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.