Wednesday, 2 July 2014

Accessing Google data, part 2

Intro

Back in Novemeber 2012, I blogged about accessing Google data from APEX - and provided some sample code to getting started. You can find this post here: http://apextips.blogspot.com.au/2012/11/accessing-google-data.html.

Over the past little while, I have been (slowly) working on a more modularised API, that can be easily built upon to add support for additional Google services. I have called it PL/GAPI (Google API). It can be found on github: https://github.com/trent-/pl-gapi.

I hope the wiki on github is straightforward enough to follow in setting this up in your local environment: https://github.com/trent-/pl-gapi/wiki

Services

I've started with the following services:
  • Drive
  • Calendar
Usage

Without going into too much detail (since it's all on the wiki linked earlier), you link to an authorization URL with: 

&OWNER..GAPI_AUTH.BEGIN_AUTH?p_scope=&P0_SCOPE.&p_return_app=&APP_ID.&p_return_page=&APP_PAGE_ID.&p_session=&APP_SESSION.&p_item_for_refresh_token=GOOGLE_REFRESH_TOKEN

This tells the system where to return to and P0_SCOPE defines what scope you are requesting authorization for. Current scopes defined:
  • GAPI_DRIVE.SCOPE_FULL
  • GAPI_CAL.SCOPE_FULL
  • GAPI_CAL.SCOPE_READ_ONLY

Once the user accepts, they are returned to the application in the parameters specified in the above redirect (p_return_app, p_return_page) along with the item to store the refresh token in.

Refresh token is a means to be able to get the access token without continually prompting the user for access. All requests require a valid access token to complete successfully. Because the returned refresh token contains the '/' character, it is escaped by APEX. Before storing it, you need to un-escape the string. To do this, I used the function: UTL_I18N.UNESCAPE_REFERENCE. 

Once we have the refresh token, we can easily get the access token with:

gapi_auth.get_access_token(:GOOGLE_REFRESH_TOKEN);

Read more about the implementation here: https://github.com/trent-/pl-gapi/wiki/Authorization

Then for example, we can add a new folder to our drive:

    :P2_RETURN_FOLDER_ID :=
        gapi_drive_file.create_folder(
            p_folder_name       => :P2_NEW_FOLDER_NAME
          , p_access_token      => :GOOGLE_ACCESS_TOKEN);


I've set up a few examples on the wiki, but really, at this stage, the sample application in the samples folder is the best tool to refer to for sample usage

Feedback

Any feedback, criticisms, suggestions about the implementation are more than welcomed.

note: It's a long way from being complete, but just thought it was time to share.

No comments:

Post a Comment