How to generate auth_code and token_code

How to generate auth_code and token_code


In this article, you can see how to generate the auth_code and token_code, necessary to use the  sc_api_upload and sc_api_download.
These keys must be generated individually, follow the steps below to generate them without error.

To use the upload and download macros with google drive, you must enable google drive api in your google account.

Generate auth_code with sc_api_gc_get_url macro

The sc_api_gc_get_url macro generates a URL for google account user authentication used for API configuration.
It has two parameters:
  1. app_name - Receive the app_name, available in your api configuration. This parameter accepts strings or variable.
  2. json_oauth - Receive the content of the json file, this file is available for download in your api settings. This parameter accepts strings or variable.
Follow the steps bellow.

1. Create a blank application in your project, with the objective of obtaining the auth_code.



2. In the onExecute event, add the code below.
The values assigned to the variables $app_name and $json_oauth, in the example below, are for illustration only and should be changed by your account data.


Code example

  1. // $app_name should receive the app name configured in your google account
  2. $app_name  = 'scriptcase';
  3. // $json_oauth should receive the contents of the json file available for download in your google app's settings.
  4. $json_oauth  = '{"installed":{"client_id":"238650139811-eqrje3lqhj9a404iuduh9l5kvk5i13ng.apps.googleusercontent.com","project_id":"sample-294791","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x519_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"E-Z_t8PU-8u0BFwumHRorbTl","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}';

  5. // Generates the account authentication link
  6. $url = sc_api_gc_get_url($app_name$json_oauth);
  7. // Redirects the app to the link generated by the macro
  8. sc_redir($url);

Google authentication

3. Then run the application.
You will be asked to login with the account used to configure the API.



4. After login, access permission will be requested:


4. Then the code will be generated.
Store the code in a txt file, it will be needed for us to generate the token_code.



Possible errors

This error occurs when the value informed in the json_oauth parameter is wrong.
Open the json file again, copy the entire contents of the file, it must be informed in full.




Generate token_code with sc_api_gc_get_obj macro

The sc_api_gc_get_obj macro generates the token_code.
It has four parameters:
  1. app_name - Receive the app_name, available in your api configuration. This parameter accepts strings or variable.
  2. json_oauth - Receives the content of the json file, this file is available for download in your API settings. This parameter accepts strings or variable.
  3. auth_code - Receives the auth_code generated in the previous step or another valid auth_code that you have already generated.
Follow the steps below.
This walkthrough should only be performed for each auth_code generated. If any problem occurs when generating the token_code, it will be necessary to generate a new auth_code.

In the same blank application, created to generate the auth_code, it is also possible to generate the token_code, for that follow the steps below.

5. Comment on the lines highlighted below:



Code example with the lines that must be commented out.
  1. // $app_name should receive the app name configured in your google account
  2. $app_name  = 'scriptcase';
  3. // $json_oauth should receive the contents of the json file available for download in your google app's settings.
  4. $json_oauth  = '{"installed":{"client_id":"238650139811-eqrje3lqhj9a404iuduh9l5kvk5i13ng.apps.googleusercontent.com","project_id":"sample-294791","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x519_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"E-Z_t8PU-8u0BFwumHRorbTl","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}';

  5. // Generates the account authentication link
  6. // $url = sc_api_gc_get_url($app_name, $json_oauth);
  7. // Redirects the app to the link generated by the macro
  8. // sc_redir($url);

6. After the sc_redir line, which should be commented out, add the lines below.


Code example

  1. // Receives auth_code generated in the previous step
  2. $auth_code = '4/1AY0e-g4BjstStsxAJd7A5jjlzBifl5AWSS9x04xhwp6Cyh2JON0i3r2oNd4';

  3. $auth_token = sc_api_gc_get_obj($app_name$json_oauth$auth_code);
  4. echo $auth_token;
The code should be looking like this at the moment.



7. Then run the application. The token_code value must be displayed on the screen.
Store the token in a .txt file.

Example of generated token_code:
  1. {"access_token":"ya29.a1AfH6SMBSExys1mg0_4aaZ1H8iVNUtZ-2iid4zIjvywOpPDpL5_fhpuYu_Evgqj14Zll1mu0DKeX-UPBBbkIY33T_hIwUKNi6h5GVmE8RSt7vwj1N8rmM_ae3EjOBMDIaS_F2vkZ8ZzpnBA0V38WwYBxBNR1C","expires_in":3599,"refresh_token":"1\/\/0heh4c5k0p_OqCgYIARAAGBESNwF-L2IrTOLtSoBOr-qdK4gDtSkyXtLYh99S1DGG6Y0x6sBwNu2sVnlqEFczG7-zEIBvCd8Bb6Q","scope":"https:\/\/www.googleapis.com\/auth\/drive","token_type":"Bearer","created":1623154166}

The sc_api_gc_get_obj macro must be run only once for each auth_code generated.
After getting the token_code, the auth_token parameter must be changed so that a new token_code is obtained. Otherwise, Fatal error: Uncaught InvalidArgumentException: Invalid token format, described below, will be displayed when running the application.

Possible errors when generating token_code.

Fatal error: Uncaught InvalidArgumentException: Invalid token format

This error occurs when the sc_api_gc_get_obj macro is run a second time without the auth_code values having been changed.
In this case, it will be necessary to generate a new auth_code, to get a new one as seen at the beginning of the article. Return to step Generate auth_code with sc_api_gc_get_url macro




    • Related Articles

    • How to make a report in PDF using code

      Small tutorial tomake a report in PDF. Other examples canbe taken from https://tcpdf.org/examples/ The scriptcase toolalso has the respective macros to ...
    • Create a folder using PHP code

      To create a folder on the server partition, use the PHP command: mkdir (); In the example below, I will create a folder where your name will be the current system date. <php $folder = date("Ymd");mkdir ($folder, 0755); ?> The value "0755", will be ...
    • See how to generate recaptcha credentials

      ReCAPTCHA is an API provided by Google for forms. It adds security, preventing automatic submission of forms through robots. reCAPTCHA sample: First, we must request an API Key to activate reCAPTCHA into a Scriptcase application by following the ...
    • Configuring SMS API in ScriptCase

      In ScriptCase, we can configure and enable 3 types of SMS API natively. They are: Twilio, Plivo and Ckickatell. For this article, we will use the Twilio SMS API as an example, where we have the possibility to perform a free trial, as well as the ease ...
    • Enabling Two-Factor Authentication (2FA) in the Security of our Project

      Two-factor authentication (2FA) provides more security for projects developed in ScriptCase, as well as for users of this project. In this article, we'll look at how to enable the use of two-factor authentication (2FA) when creating the project's ...
    • Popular Articles

    • Examples for diagrams

      This tutorial shows how you can import and use the new examples for diagrams released with Scriptpcase V9.2.16.  The applications were created using the components: Jsmind, JKanban e Orgchart.  - Sample 1: Mind Map diagram Click here to  execute the ...
    • Manual Update - Windows

      Before performing any procedure we recommend performing a backup of your projects. To back up Scriptcase: Go to Settings > Administration, in the side menu go to Services > Backup. A file (.ZIP) will be generated with all your projects, save this ...
    • PHP functions necessary for ScriptCase

      In PHP , there are some essential functions for the operation of Scriptcase .  On some servers , these functions are disabled , this makes the Scriptcase not work properly.  If you're using a shared hosting server, you'll need to contact the host ...
    • Oracle Connection - Scriptcase 9 - Linux 64 Bits

      This procedure will only work if you have a Scriptcase manual installation in your server. Installing PHP 7.0 Installing the Scriptcase Manually To connect with Oracle in a Linux System, we must follow these steps below: Step 1 -  Installing these ...
    • Using friendly URL

      The friendly URL was included from Scriptcase 7, in order to make the URL, of the applications generated by Scriptcase, cleaner and easier to associate them with applications. For example, using friendly URLs the link to a Scriptcase application ...