无法在Stripe中接收访问令牌

时间:2016-07-18 19:14:14

标签: php oauth stripe-payments access-token

我是Stripe整合新手。我已经阅读了Stripe的API文档,这是OAuth流程。但我仍然没有收到任何OAuth访问令牌。有人可以解释我如何才能收到访问令牌?谢谢!



if (isset($_GET['code'])) { // Redirect w/ code
  $code = $_GET['code'];

  $token_request_body = array(
    'grant_type' => 'authorization_code',
    'client_id' => 'ca_32D88BD1qLklliziD7gYQvctJIhWBSQ7',
    'code' => $code,
    'client_secret' => ''
  );

  $req = curl_init(TOKEN_URI);
  curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($req, CURLOPT_POST, true );
  curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($token_request_body));

  // TODO: Additional error handling
  $respCode = curl_getinfo($req, CURLINFO_HTTP_CODE);
  $resp = json_decode(curl_exec($req), true);
  curl_close($req);

  echo $resp['access_token'];
} else if (isset($_GET['error'])) { // Error
  echo $_GET['error_description'];
} else { // Show OAuth link
  $authorize_request_body = array(
    'response_type' => 'code',
    'scope' => 'read_write',
    'client_id' => 'ca_32D88BD1qLklliziD7gYQvctJIhWBSQ7'
  );

  $url = AUTHORIZE_URI . '?' . http_build_query($authorize_request_body);
  echo "<a href='$url'>Connect with Stripe</a>";
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您应该使用OAuth 2.0客户端库,而不是像Stripe建议的那样尝试自己滚动它: https://stripe.com/docs/connect/standalone-accounts#sample-code

有很多这些,但这是一个非常好的选择: https://github.com/thephpleague/oauth2-client

您可以修改此示例并检索帐户ID,如下所示:

$provider->getResourceOwner($accessToken)->getId();

检索帐户ID后,您将存储并使用此帐户作为Stripe建议的已连接帐户进行身份验证: https://stripe.com/docs/connect/authentication#authentication-via-the-stripe-account-header

答案 1 :(得分:0)

他们实际上有一个官方的github库

他们有一个关于oauth事的例子

因为某种原因在文档中丢失了......

https://github.com/stripe/stripe-php/blob/master/examples/oauth.php

如果他们删除它,我在这里包含文件,注意:他们使用他们的库,所以你必须在此之前安装它

<?php

require('../init.php');

\Stripe\Stripe::setApiKey(getenv('STRIPE_SECRET_KEY'));
\Stripe\Stripe::setClientId(getenv('STRIPE_CLIENT_ID'));


if (isset($_GET['code'])) {
    // The user was redirected back from the OAuth form with an authorization code.
    $code = $_GET['code'];

    try {
        $resp = \Stripe\OAuth::token([
            'grant_type' => 'authorization_code',
            'code' => $code,
        ]);
    } catch (\Stripe\Error\OAuth\OAuthBase $e) {
        exit("Error: " . $e->getMessage());
    }

    $accountId = $resp->stripe_user_id;

    echo "<p>Success! Account <code>$accountId</code> is connected.</p>\n";
    echo "<p>Click <a href=\"?deauth=$accountId\">here</a> to disconnect the account.</p>\n";

} elseif (isset($_GET['error'])) {
    // The user was redirect back from the OAuth form with an error.
    $error = $_GET['error'];
    $error_description = $_GET['error_description'];

    echo "<p>Error: code=" . htmlspecialchars($error, ENT_QUOTES) . ", description=" . htmlspecialchars($error_description, ENT_QUOTES) . "</p>\n";
    echo "<p>Click <a href=\"?\">here</a> to restart the OAuth flow.</p>\n";

} elseif (isset($_GET['deauth'])) {
    // Deauthorization request
    $accountId = $_GET['deauth'];

    try {
        \Stripe\OAuth::deauthorize([
            'stripe_user_id' => $accountId,
        ]);
    } catch (\Stripe\Error\OAuth\OAuthBase $e) {
        exit("Error: " . $e->getMessage());
    }

    echo "<p>Success! Account <code>" . htmlspecialchars($accountId, ENT_QUOTES) . "</code> is disconnected.</p>\n";
    echo "<p>Click <a href=\"?\">here</a> to restart the OAuth flow.</p>\n";

} else {
    $url = \Stripe\OAuth::authorizeUrl([
        'scope' => 'read_only',
    ]);
    echo "<a href=\"$url\">Connect with Stripe</a>\n";
}