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 dashboardLike 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 functionIf 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
Once you've run this, you're good to go. 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. |
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > OAuth2 for Apps Script in a few lines of code >