The Paypal Rest API uses OAuth2 a little like a Service Account, which means there’s typically no user dialog. It uses a grant type called client_credentials. Goa now supports this kind of OAuth2 flow. You’ll need the latest Goa library.
Goa library is available with this key.
   MZx5DzNPsYjVyZaR67xXJQai_d-phDA33

Paypal dashboard

Like other APIS, there’s a dashboard at which you need to create an application and get credentials – it looks like this

You can create both Sandbox and live applications. These examples will mainly be using the Sandbox versions.

One off function

If you’re familiar with Goa, you’ll know that there is a one off function you create to store your credentials, which can be deleted after running. For Paypal it looks like this.

cGoa.GoaApp.setPackage (PropertiesService.getScriptProperties() , {
    clientId:'A..............Sh',
    clientSecret:'EJ...........SsYL',
    account:'br............m',
    packageName: 'paypalsandbox',
    scopes : ["https://api.sandbox.paypal.com/v1/payments/.*"],
    service:'paypal_sandbox',
    reset:new Date().getTime()
  });
The main points are
  • The id, account and secret come from the paypal console
  • Goa provides 2 services  – paypal_sandbox and paypal_live . This example is using paypal_sandbox
  • I’ve called this particular profile ‘paypalsandbox’. You can call it what you like.
  • This scope is set for the payments API. Just add the ones you need. I’ve noticed that the API in the Sandbox doesn’t seem to pay much attention to this and works anyway, but I’ve implemented as I understand the documentation to be claiming. I’ll update this later if I find out some more things about scope.
Once you’ve run this, you’re good to go.

To use

Since there is no user interaction required for this, it’s very simple to initialize. Goa automatically takes care of refreshing tokens when required.
var goa = cGoa.make('paypalsandbox', PropertiesService.getScriptProperties());

and the token can be used as in this example of creating a payment request.

var response = UrlFetchApp.fetch("https://api.sandbox.paypal.com/v1/payments/payment", {
    contentType:"application/json",
    method:"POST",
    muteHttpExceptions:true,
    headers:{
      Authorization:"Bearer " + goa.getToken()
    },
    payload:JSON.stringify(payload)
  });
For more like this, see Google Apps Scripts Snippets

Why not join our forum, follow the blog or follow me on Twitter to ensure you get updates when they are available.