zechdc/oauth1-etrade

PHP League OAuth1-Client 的 ETrade OAuth 1.0 客户端提供者

1.3 2017-11-26 01:05 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:01:34 UTC


README

此包为 PHP League 的 OAuth 1.0 客户端 提供了 Etrade OAuth 1.0 支持。

安装

要安装,请使用 composer

composer require zechdc/oauth1-etrade

使用方法

使用方法与 The League 的 OAuth 客户端相同,使用 Zechdc\OAuth1\Client\Server\Etrade 作为提供者。

您必须联系 Etrade 以设置您的回调 URL。

//Step 1, setup an Etrade instance
$server = new Zechdc\OAuth1\Client\Server\Etrade(array(
    'identifier'   => 'oauth_customer_key',
    'secret'       => 'consumer_secret',
));

//Step 2, get create your Request Token ($temporaryCredentials)
public function getRequestTokenAndAuthorizeApplication(){

    //This creates your Request Token
    $temporaryCredentials = $this->server->getTemporaryCredentials();
    
    //Save the $temporaryCredentials in a session or DB to be used later.
    Session::set('temporary_credentials', $temporaryCredentials);
    
    //This will allow the user to Authorize your Application. It will redirect the user
    //to etrade. After they login and accept your application, it will either
    // 1) Redirect to your website - this requires you to contact etrade customer support and setup a callback url
    // 2) Etrade will show you a code called the oauth_verifier which you can manually copy into the next step.
    $this->server->authorize($temporaryCredentials);
}

//Step 3, use the request token ($temporaryCredentials) and the oauth_verifier provided by etrade to create your Access Token
public function getAccessToken(){
    //Get our temporary credentials from our storage
    $temporaryCredentials = Session::get('temporary_credentials');
    $requestToken = $temporaryCredentials->getIdentifier();
    
    //The code the user received after authorizing the application.
    $oauthVerifier = $_GET['oauth_verifier'];
    
    //This gets our Access Token
    $tokenCredentials = $this->server->getTokenCredentials($temporaryCredentials, $requestToken, $oauthVerifier);
    
    //Save the Access Token so we can make and authorize more API calls. 
    Session::set('token_credentials', $tokenCredentials);
}

//Step 4, now that you have your Access Token, lets call an endpoint
public function getMarketData(){
    $client = new Guzzle\Client();
    $accessToken = Session::get('token_credentials');
    $url = "https://etwssandbox.etrade.com/market/sandbox/rest/quote/GOOGL.json";
    $method = 'GET';
    $params = ['detailFlag' => 'FUNDAMENTAL'];
    
    //This constructs our Authorization header and the oauth signature.
    $headers = $this->server->getHeaders($accessToken, $method, $url, $params);
  
    $res = $client->request($method, $url, [
      'headers' => $headers,
      'query' => $params
    ]);
    
    echo $res->getStatusCode();
    echo $res->getBody();
}