GPM 66

You might also like

You are on page 1of 1

);

Earlier, LoginTransaction’s initialize method had set up a call‐


back to handle OAuth messages:
Comm.registerForOAuthMessages(method(:accessCodeResult));

The makeOAuthRequest call will cause the login prompt to appear in


Garmin Connect Mobile. Once the user has completed credential
entry, the accessCodeResult callback is invoked with the response
from the authenticating server, including the temporary access code.
At this point, the user is done with the browser on her phone, and
your app will use makeWebRequest to request the access token with
an HTTP POST request. If this request is successful, it will call our
delegate’s handleResponse method with the access token:
// Convert the authorization code to the access token
function getAccessToken(accessCode) {
// Request an access token via POST over HTTPS
Comm.makeWebRequest(
// URL
"https://www.strava.com/oauth/token",
// Post parameters
{
"client_secret"=>$.ClientSecret,
"client_id"=>$.ClientId,
"code"=>accessCode
},
// Options to the request
{
:method => Comm.HTTP_REQUEST_METHOD_POST
},
// Callback to handle response
method(:handleAccessResponse)
);
}

// Callback to handle receiving the access code


function handleAccessResponse(responseCode, data) {
// If we got data back then we were successful. Otherwise
// pass the error on to the delegate
if( data != null) {
_delegate.handleResponse(data);
} else {
Sys.println("Error in handleAccessResponse");
Sys.println("data = " + data);
_delegate.handleError(responseCode);
}
}

54 | Chapter 4: Projects

You might also like