使用AJAX生成Dwolla访问令牌,而不是重定向到他们的网页


Generate a Dwolla access token using AJAX instead of redirecting to their webpage?

查看Dwolla的API文档并在我的站点上尝试oauth.php示例代码(下面显示的代码),我不清楚是否可以在不重定向到Dwolla页面的情况下生成访问令牌。

从UI/UX的角度来看,从我的网站重定向到他们的网站再到我的网站真的很糟糕,比Paypal提供的蹩脚界面好不到哪里去。

有人知道如何使用AJAX生成Dwolla访问令牌吗?



<?php
// Include the Dwolla REST Client
require '../lib/dwolla.php';
// Include any required keys
require '_keys.php';
// OAuth parameters
$redirectUri = 'http://localhost:8888/oauth.php'; // Point back to this file/URL
$permissions = array("Send", "Transactions", "Balance", "Request", "Contacts",   "AccountInfoFull", "Funding");
// Instantiate a new Dwolla REST Client
$Dwolla = new DwollaRestClient($apiKey, $apiSecret, $redirectUri, $permissions);
/**
 * STEP 1: 
 *   Create an authentication URL
 *   that the user will be redirected to
 **/
if(!isset($_GET['code']) && !isset($_GET['error'])) {
$authUrl = $Dwolla->getAuthUrl();
header("Location: {$authUrl}");
}
/**
 * STEP 2:
 *   Exchange the temporary code given
 *   to us in the querystring, for
 *   a never-expiring OAuth access token
 **/
 if(isset($_GET['error'])) {
echo "There was an error. Dwolla said: {$_GET['error_description']}";
}
else if(isset($_GET['code'])) {
$code = $_GET['code'];
$token = $Dwolla->requestToken($code);
if(!$token) { $Dwolla->getError(); } // Check for errors
else {
    session_start();
    $_SESSION['token'] = $token;
    echo "Your access token is: {$token}";
} // Print the access token
}

TL;DR -不,这不是OAuth的工作方式


OAuth方案的重点是在您想要使用的服务的网站上进行身份验证,在本例中是Dwolla。通过强制用户进入他们的页面,它可以确保以下几点:

  1. 让用户知道他们正在使用的外部服务的服务条款可能与您的应用程序不同
  2. 让用户知道应用程序为该服务请求的功能。在dwolla的例子中,你的应用程序可以请求不同级别的功能,包括转账,所以让你的用户意识到这一点很重要!

您可以在http://oauth.net/上阅读更多关于OAuth的信息