uris77
1/12/2013 - 4:53 PM

Obtaining request token from freshbooks with groovy

Obtaining request token from freshbooks with groovy


   

    def index() {
        String url = "https://sample.freshbooks.com/oauth/oauth_request.php"
        HttpClient httpClient = new DefaultHttpClient()
        def params = [                
                oauth_consumer_key: URLEncoder.encode("mdlabs", "UTF-8"),
                oauth_callback: URLEncoder.encode("http://127.0.0.1:8080/FreshbooksOauth/oauth/callback", "UTF-8"),
                oauth_version: URLEncoder.encode("1.0", "UTF-8"),
                oauth_nonce: URLEncoder.encode("12345678901234567890", "UTF-8"),
                oauth_signature_method: 'PLAINTEXT',
                oauth_signature: URLEncoder.encode('23ADFDdadffaa123123&', 'UTF-8'),
                oauth_timestamp: System.currentTimeMillis()/1000
        ]
        String request_url = url + "?"

        def flatParams = params.collect{key, value->
            "${key}=${value}"
        }.join("&")
        request_url += flatParams
        HttpPost httpPost = new HttpPost(request_url)        
        HttpResponse response =  httpClient.execute(httpPost)
        println "response: ${response.getEntity().getContent()}"
        render view: "index", model: [login_href: request_url]
    }