In this article, you can see how to generate the **auth\_code** and **token\_code**, necessary to use the  [sc\_api\_upload](https://www.scriptcase.net/docs/en_us/v9/manual/14-macros/02-macros/#sc_api_upload) and [sc\_api\_download](https://www.scriptcase.net/docs/en_us/v9/manual/14-macros/02-macros/#sc_api_download).

These keys must be generated individually, follow the steps below to generate them without error.

![](https://img.zohostatic.com/zde/static/images/info.png)

To use the upload and download macros with google drive, you must [enable google drive api](https://help.scriptcase.net/portal/en/kb/articles/enabling-google-drive-api-on-the-google-console) 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**.

![](https://help.scriptcase.net/galleryDocuments/edbsnc1ffac04cbc67b047798cc6d10c37ecf2f5df26fa272676aeebf30dcdd171342678937f5a18ac41cd0b3ba2d18f84e71?inline=true)

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.

![](https://help.scriptcase.net/galleryDocuments/edbsn03ee6c590669904f06b0dd532f3b9254c14c0314b96ffe377bd45d3da02e1dd4c6d9c42859516c63cccccb664c624c10?inline=true)

### 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"]}}';
6. // Generates the account authentication link
7. $url = sc\_api\_gc\_get\_url($app\_name, $json\_oauth);
8. // Redirects the app to the link generated by the macro
9. sc\_redir($url);

### Google authentication

3. Then run the application.

You will be asked to login with the account used to configure the API.

![](https://desk.zoho.com/DocsDisplay?zgId=716875472&mode=inline&blockId=o6tif077e3695a5bc416cbe775fa800d36c19)

4. After login, access permission will be requested:

![](https://desk.zoho.com/DocsDisplay?zgId=716875472&mode=inline&blockId=o6tifdd6d2c2ab2de41cc935e0fbc5ec51f96)

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.

![](https://desk.zoho.com/DocsDisplay?zgId=716875472&mode=inline&blockId=4mw56fdd84a325b6f4c95a326fb086e340035)

### 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.

![](https://help.scriptcase.net/galleryDocuments/edbsnfa94f939a262656b358a4ba5af99c53b5f6d669337abe8983d29e01566a4d8ff4b81a8a12226257cefdc5127045038c4?inline=true)

## 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.

![](https://img.zohostatic.com/zde/static/images/info.png)

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:

![](https://help.scriptcase.net/galleryDocuments/edbsncfc61b46bb2ab97e2ee4f1442241b694aa638e64183a4996bdda8edf44a8a23044e60d70fe4d583cc6f0f76781b7a284?inline=true)

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"]}}';
6. // Generates the account authentication link
7. // $url = sc\_api\_gc\_get\_url($app\_name, $json\_oauth);
8. // Redirects the app to the link generated by the macro
9. // sc\_redir($url);

6. After the sc\_redir line, which should be commented out, add the lines below.

![](https://help.scriptcase.net/galleryDocuments/edbsn03ee6c590669904f06b0dd532f3b925452a3b1316ae0720eb83c2d407aab770400bb9c0491d8fbdc092df13c7bd16f00?inline=true)

### Code example

1. // Receives auth\_code generated in the previous step
2. $auth\_code = '4/1AY0e-g4BjstStsxAJd7A5jjlzBifl5AWSS9x04xhwp6Cyh2JON0i3r2oNd4';
4. $auth\_token = sc\_api\_gc\_get\_obj($app\_name, $json\_oauth, $auth\_code);
5. echo $auth\_token;

The code should be looking like this at the moment.

![](https://help.scriptcase.net/galleryDocuments/edbsn7166d45c961a29fca506520a7c9752f88bc4c34325d474d18e4a6883832d58c85f39c01fccf5a9427d48e59fdd43560d?inline=true)

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}](http://www.googleapis.com//auth//drive%22,%22token_type%22:%22Bearer%22,%22created%22:1623154166%7D)

![](https://img.zohostatic.com/zde/static/images/caution.png)

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**

![](https://help.scriptcase.net/galleryDocuments/edbsn55f6513de1a5fd3eaa8a32388e528682fc96f97b488d736b0e712f3836af2e45cbbe180a39e56243babeeae2f1d8340f?inline=true)