Search code examples
javaauthenticationgrailsoauthfitbit

How To Get Data From Fitbit Oauth Authentication


I am implementing an OAuth authentication for Fitbit. And I have done all the process. Please refer the screen shots.

for this I have code on my controller like

class MyController {

    def myService;

    static defaultAction = "getUserDetails"
    String subscriber_id = "1";

    def getUserDetails() {
        if (request.getParameter("completeAuthorization") != null) {
            forward(action: 'showUserInfo')
        }
        else {
            try {

                response.sendRedirect(FitBitAPI.getBean().getResourceOwnerAuthorizationURL(new LocalUserDetail("-"), grailsApplication.config.myBaseUrl + "/my/showUserInfo?completeAuthorization="));
            }
            catch (FitbitAPIException e) {
                throw new ServletException("Exception during performing authorization", e);
            }

        }
    }

    def showUserInfo() {
        /*println "${params}--------------------------------"*/
        render {params}


        APIResourceCredentials resourceCredentials = myService.getCredentials(params)
        UserInfo userInfo;
        println "=====================gggggg================================================>>>>"+resourceCredentials
        try {
            userInfo = FitBitAPI.getBean()?.getClient()?.getUserInfo(new LocalUserDetail(resourceCredentials.getLocalUserId()));
            println "===================after FitBitAPI==============================================="
            [userInfo: userInfo]
            println "===================user Info====================================================="

        } catch (FitbitAPIException e) {
            throw new ServletException("Exception during getting user info", e);
        }
        [params:params]
    }
}

At first I create a link on my index page and on click getUserDetails() call like ----- enter image description here

and when I click on this link Fitbit the OAuth page open like.. enter image description here

and after passing all the credential i have found an error -- enter image description here


Solution

  • I use fitbit4j and the problem was solved.