With GreenTools for Google Apps (G4G) v15.00 and up you are now able to complete service registration for accounts using ILE functions instead of the G4GREGSVC command. This is useful for shops that have custom screens or are web based only, and don't have the option of running commands from a command line.
We have also set up a RESTful API endpoint that allows you to send us your unique state code and a URL that you would like our site to redirect to when registration is complete.
The new functions are used as follows:
// get a unique Code and URL
g4g_setValue('id':'bvstone@gmail.com');
g4g_setValue('service':'*DRIVE');
rc = g4g_getRegistrationURL(outStateCode:outURL:errMsg);
Once this is called, the g4g_getRegistrationURL function will return two very important pieces of information:
{
"state_code":"C4XPgffaaCNV2BiROAJMG2xx",
"redirect_url":"https://www.yourpath.com/endpoint?field1=data1&field2=data2..."
}
The state_code value will be the code returned in the outStateCode output paramter of the g4g_getRegistrationURL() function. The redirect_url value will be where you would like the user redirected to once things are complete. This can contain any information you wish returned to your endpoint. If you are passing data as a query string, it is best that the data is URL encoded, especially if it contains any special characters.
An example using GETURI to set up this redirect URL would be similar to the following:
// build JSON into stream file or variable using YAJL, or a string, etc.
jsonData = '{"state_code":"' + %trim(outStateCode) + '",' +
'"redirect_url":"https://www.yourpath.com/endpoint?field1=data1&field2=data2..."}';
geturi_resetValues();
geturi_setValue('gi_uri':'[contact us for endpoint]');
geturi_setValue('gi_port':'443');
geturi_setValue('gi_ssl':'*YES');
geturi_setValue('gi_reqmeth':'POST');
geturi_setValue('gi_conttype':'application/json');
geturi_setValue('gi_data':jsonData);
geturi_setValue('gi_sprhead':'*YES');
geturi_setValue('gi_outtype':'*RETURN');
rc = geturi_call(GetUri_Out:GetUri_Head:GetUri_MsgCd:GetUri_Msg:errMsg);
response = geturi_getResponseCode(); //ie 500, 200, etc
//load into YAJL example
docNode = yajl_buf_load_tree(%addr(GetUri_Out:*DATA):
%len(GetUri_Out):yajl_errMsg);
A content-type of application/json is required as well as properly formatted JSON.
This call will return JSON and either success or error as follows:
Success:
{
"response":"success",
"state_code":"[the state code you passed in]"
}
Error:
{
"response":"error",
"error":"reason for error"
}
After the user has completed the accepting permissions required for G4G through Google, you now need to retrieve the OAuth 2.0 token (as well as other information) so it can be placed in the setup files inside G4G.
// exchange the code for a token and set up in G4G
g4g_setValue('id':'bvstone@gmail.com');
g4g_setValue('service':'*DRIVE');
g4g_setValue('state_code':outStateCode);
rc = g4g_exchangeCodeForToken(errMsg);
As shown, we are sending the account information, the service and the outStateCode returned from g4g_getRegistrationURL() to the g4g_exchangeCodeForToken() function. This will take the state code (which is NOT the same as the OAuth 2.0 code returned from Google), make a web service call to get the OAuth 2.0 code, then call a Google API to exchange that code for a token, refresh token, and other information. The user will then be set up in G4G and as long as the account is active, refreshing the token will be taken care of automatically by G4G when calling any of the functions inside of the G4G Addon Libraries.
See the GreenTools for Google Apps (G4G) documentation for a full set of instructions and settable variables for each of these new functions.
As always, please contact us with any questions.