{"id":2151,"date":"2021-02-24T18:08:56","date_gmt":"2021-02-24T10:08:56","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=2151"},"modified":"2021-02-24T18:08:56","modified_gmt":"2021-02-24T10:08:56","slug":"how-to-get-an-access-token-to-integrate-with-paypal-rest-apis-in-python","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-get-an-access-token-to-integrate-with-paypal-rest-apis-in-python\/","title":{"rendered":"How to get an Access Token to integrate with PayPal REST APIs in Python"},"content":{"rendered":"<p>Before you can integrate with PayPal via the <a href=\"https:\/\/developer.paypal.com\/docs\/api\/overview\" rel=\"noopener\" target=\"_blank\">REST APIs<\/a>, you will need to obtain an access token.<\/p>\n<p>Given that REST APIs is agnostic to programming languages, you can use a programming language of your choice to get the Access Token.<\/p>\n<p>In case you are wondering how you can get an Access Token to integrate with PayPal REST APIs in Python, this is how you can do so.<\/p>\n<h2>1. Create a PayPal App for your application to integrate with<\/h2>\n<p>If you have not done any integration with PayPal before, you will need to create a PayPal App.<\/p>\n<p>In order to do so, <a href=\"https:\/\/www.paypal.com\/signin?returnUri=https%3A%2F%2Fdeveloper.paypal.com%2Fdeveloper%2Fapplications\" rel=\"noopener\" target=\"_blank\">login to PayPal developer account<\/a>. After you had logged in successfully, click on <a href=\"https:\/\/developer.paypal.com\/developer\/applications\/create\" rel=\"noopener\" target=\"_blank\">Create App<\/a> under <strong>My apps & credentials<\/strong>:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PayPal-developer-portal-My-apps-and-credentials-screen-with-Create-App-button-highlighted.gif\" alt=\"PayPal developer portal My apps and credentials screen with Create App button highlighted\" class=\"aligncenter\" \/><\/p>\n<p>If you are creating a PayPal App for production integration, then be sure to toggle the Live option at the top.<\/p>\n<p>When the <strong>Create New App<\/strong> page loads, fill in your App details and associate it to a PayPal business account.<\/p>\n<p>After you clicked on <strong>Create App<\/strong>, you will see the Client ID and Secret:<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PayPal-App-my-app-details-page.gif\" alt=\"PayPal App my-app details page\" class=\"aligncenter\" \/> after the App is created. <\/p>\n<p>You can get the secret by clicking on <strong>Show<\/strong>.<\/p>\n<p>Given that, record the <strong>Client ID<\/strong> and <strong>Secret<\/strong> for later use.<\/p>\n<h2>2. Install the Python requests library<\/h2>\n<p>If you had not done so, you can install the Python <a href=\"https:\/\/requests.readthedocs.io\/en\/master\/\" rel=\"noopener\" target=\"_blank\">requests<\/a> library into your Python environment:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install requests\r\n<\/pre>\n<p>After you had installed the requests library, you will have an easy-to-use library to send our <a href=\"https:\/\/www.techcoil.com\/glossary\/http-request\" rel=\"noopener\" target=\"_blank\">HTTP requests<\/a>.<\/p>\n<h2>3. Run the Python codes to get the Access Token from your PayPal App<\/h2>\n<p>Given that we have the necessary ingredients, we are ready to write the Python codes to get the Access Token.<\/p>\n<p>To get the Access Token, we are <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-send-an-http-request-to-a-http-basic-authentication-endpoint-in-python-3-with-requests-library\/\">sending a HTTP Basic Authentication request with Python<\/a> to PayPal.<\/p>\n<p>Given that in mind, the following is a simple Python script to get the the Access Token from your PayPal App:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport requests\r\n\r\n# Change the URL to https:\/\/api-m.paypal.com for live access\r\nPAYPAL_URL = 'https:\/\/api-m.sandbox.paypal.com'\r\nAPP_CLIENT_ID = 'your_app_client_id'\r\nAPP_SECRET = 'your_app_secret'\r\n\r\noauth_url = '%s\/v1\/oauth2\/token' % PAYPAL_URL\r\n\r\noauth_response = requests.post(oauth_url,\r\n                               headers= {'Accept': 'application\/json',\r\n                                         'Accept-Language': 'en_US'},\r\n                               auth=(APP_CLIENT_ID, APP_SECRET),\r\n                               data={'grant_type': 'client_credentials'})\r\n\r\n# Get OAuth JSON in response body\r\noauth_body_json = oauth_response.json()\r\n# Get access token\r\naccess_token = oauth_body_json&#x5B;'access_token']\r\nprint(access_token)\r\n<\/pre>\n<p>Before running the script, change <strong>your_app_client_id<\/strong> and <strong>your_app_secret<\/strong> with the ones that you obtained earlier. After you have done so, you will be able to see the Access Token on your shell console when you run the script.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-yH\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-yH&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-yH&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F2151&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Before you can integrate with PayPal via the <a href=\"https:\/\/developer.paypal.com\/docs\/api\/overview\" rel=\"noopener\" target=\"_blank\">REST APIs<\/a>, you will need to obtain an access token.<\/p>\n<p>Given that REST APIs is agnostic to programming languages, you can use a programming language of your choice to get the Access Token.<\/p>\n<p>In case you are wondering how you can get an Access Token to integrate with PayPal REST APIs in Python, this is how you can do so.<\/p>\n","protected":false},"author":1,"featured_media":2154,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[403,59,753,226,551],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-logo-and-PayPal-logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-yH","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/2151"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=2151"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/2151\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/2154"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=2151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=2151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=2151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}