PayPal REST API - Sandbox为API请求返回401但成功访问令牌

时间:2013-08-19 21:35:26

标签: rest paypal paypal-sandbox

我正在使用Java中的PayPal REST API向测试(沙盒)服务器请求访问令牌,该令牌成功,然后将该访问令牌发送回相同的沙箱服务器,以便付款失败401未授权。

我按照此处的PayPal REST API说明进行操作:https://developer.paypal.com/webapps/developer/docs/api/#authentication--headers和此处https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/

我尝试使用OAuthTokenCredential对象提供clientIdclientSecret,但它只返回访问令牌,而不是PayPal提供的appID和{{} 1分钟的时间。此外,它抱怨我的类路径中缺少expiresIn文件。

然后我创建了自己的类来使用Apache的HttpClient进行调用以获得完整的响应并且有效:

RestEasy的自定义PayPalAPI界面

sdk_conf.properties

自定义OAuth令牌请求

@Path("v1/oauth2/token")
public interface PayPalOAuthAPI {
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public OAuthInfo requestAccessToken(
        @HeaderParam("Authorization") String basicEncodedToken, 
        String requestBody
    );
}

这一切都正常工作,并返回200 OK状态,包含访问令牌的预期JSON响应。

然后我接受该令牌并将其提供给我的private OAuthInfo getOAuthInfo() { OAuthInfo info = null; PayPalOAuthAPI client = HttpClientFactory.createRestEasyClient(80, 443, 120, 2, API_ENDPOINT, PayPalOAuthAPI.class, true); info = client.requestAccessToken("Basic "+ generateBase64String(PP_API_USER, PP_API_SECRET), "grant_type=client_credentials"); if (info == null) { throw new RuntimeException(APIResultCodes.REMOTE_UNAUTHORIZED, "Could not authenticate with PayPal."); } return info; } 对象,该对象使用PayPal的代码与他们的服务器进行通信(我相信它是基本的java.lang.net.HttpConnection代码)并返回401未经授权的

使用PayPal的REST API付款电话

Payment

日志输出

(包括带有ID编辑的HttpClient请求/响应)

// get a new token
String accessToken = getOAuthInfo().getAccessToken();

// configProps is a Properties object pre-populated with sdk_conf.properties
// values except service.EndPoint, clientID, and clientSecret
configProps.setProperty("service.EndPoint", API_ENDPOINT);

// ... transaction objects omitted

try {
    Payment payment = new Payment();
    Payment.initConfig(configProps);
    payment.setIntent(Intent.sale.toString());
    payment.setPayer(payer);
    payment.setTransactions(transactions);

    Payment createdPayment = payment.create(accessToken);
} catch (PayPalRESTException ex) {
    LOG.error("Failed to submit donation", ex);
}

我注意到API发送给我的范围和URL是两个不同的东西。也许我缺少一些配置来将API调用发送到沙箱URL?

在上面的代码中,您可以看到我将2013-08-19 16:34:11,077 [qtp1005310362-44] DEBUG:org.apache.http.impl.conn.DefaultClientConnection Sending request: POST /v1/oauth2/token HTTP/1.1 2013-08-19 16:34:11,077 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "POST /v1/oauth2/token HTTP/1.1[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "Content-Type: application/x-www-form-urlencoded[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "Accept: application/json[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "Authorization: Basic QWM1UmNoQ3g3dHB1bWZSZW9rcUR3MW41bHRXaktQU0xRODIyWUJWWXpEdXpZUGJuc0J0eDZYWGlqX1pROkVJVnQ5eERmM2JnQmw1OG5KYlZ2VmtSR3JCaVZVN1BIWGtSV01mQjVqb3NxRTNkbWxCcF9TV05BdU91eA==[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "Accept-Encoding: gzip, deflate[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "Content-Length: 29[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "Host: api.sandbox.paypal.com[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "Connection: Keep-Alive[\r][\n]" 2013-08-19 16:34:11,077 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "User-Agent: Apache-HttpClient/4.2 (java 1.5)[\r][\n]" 2013-08-19 16:34:11,078 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "[\r][\n]" 2013-08-19 16:34:11,078 [qtp1005310362-44] DEBUG:org.apache.http.wire >> "grant_type=client_credentials" 2013-08-19 16:34:12,690 [qtp1005310362-44] DEBUG:org.apache.http.wire << "HTTP/1.1 200 OK[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] DEBUG:org.apache.http.wire << "Server: Apache-Coyote/1.1[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] DEBUG:org.apache.http.wire << "Date: Mon, 19 Aug 2013 20:34:11 GMT[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] DEBUG:org.apache.http.wire << "PayPal-Debug-Id: d29c41eb8625a[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] DEBUG:org.apache.http.wire << "Content-Type: application/json[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] DEBUG:org.apache.http.wire << "Content-Length: 282[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] DEBUG:org.apache.http.wire << "[\r][\n]" 2013-08-19 16:34:12,690 [qtp1005310362-44] 2013-08-19 16:34:12,691 [qtp1005310362-44] DEBUG:org.apache.http.impl.client.DefaultHttpClient Connection can be kept alive indefinitely 2013-08-19 16:34:12,691 [qtp1005310362-44] DEBUG:org.apache.http.wire << "{"scope":"https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/vault/credit-card/.* openid","access_token":"RVATuLOQB0WFX keSDZQw4ZnyfIduPCF2j7sMhcfspwo","token_type":"Bearer","app_id":"APP-80W284285Q519543T","expires_in":28800}" 2013-08-19 16:34:12,693 [qtp1005310362-44] DEBUG:org.apache.http.impl.conn.PoolingClientConnectionManager Connection [id: 5][route: {s}->https://api.sandbox.paypal.com] can be kept alive indefinitely 2013-08-19 16:34:12,693 [qtp1005310362-44] DEBUG:org.apache.http.impl.conn.PoolingClientConnectionManager Connection released: [id: 5][route: {s}->https://api.sandbox.paypal.com][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 2] 2013-08-19 16:34:15,197 [qtp1005310362-44] ERROR:com.example.gateways.paypal.payments.PayPalPaymentsProREST Failed to submit donation com.paypal.core.rest.PayPalRESTException: Error code : 401 with response : Server returned HTTP response code: 401 for URL: https://api.sandbox.paypal.com/v1/payments/payment 设置为service.EndPoint,在我的测试中设置为API_ENDPOINT。我也没有在我的属性对象中提供https://api.sandbox.paypal.comclientID,但即使提供它们也会给我401.

我还缺少什么?

1 个答案:

答案 0 :(得分:0)

在尝试为第三方PayPal帐户付款时遇到了这个问题并遇到了类似的问题。

不确定您是否遇到了与我相同的事情,但我正在尝试为第三方paypal帐户创建付款,我没有正确请求许可。事实证明我需要使用权限API来请求适当的权限。

请参阅此链接:https://developer.paypal.com/webapps/developer/docs/classic/permissions-service/integration-guide/PermissionsAbout/