PHP and Java examples to refresh OAuth 2.0 token.
PHP Example
function runCurl($parameters, $url, $method = 'GET', $postvals = null) { $p = array(); foreach ($parameters as $k = $v) { $p[] = $k . '=' . $v; } $querystring = implode('&', $p); $myUrl = $url . '?' . $querystring; $ch = curl_init($myUrl); $headers = array("Content-Type: application/x-www-form-urlencoded"); if ($method == 'GET') { $options = array( CURLOPT_URL = $myUrl, CURLOPT_RETURNTRANSFER = 1, CURLOPT_SSL_VERIFYPEER = FALSE ); $result = curl_setopt_array($ch, $options); } else { $options = array( CURLOPT_POST = true, CURLOPT_POSTFIELDS = $postvals, CURLOPT_HTTPHEADER = $headers, CURLOPT_RETURNTRANSFER = 1, CURLOPT_SSL_VERIFYPEER = FALSE, CURLOPT_HTTPAUTH = CURLAUTH_ANY ); $result = curl_setopt_array($ch, $options); } $response = curl_exec($ch); curl_close($ch); return $response; } function refreshAccessToken($client_id,$client_secret,$refresh_token,$urlToken) { $postvals = array( "client_id" = $client_id, "client_secret" = $client_secret, "grant_type" = 'refresh_token', "refresh_token" = "$refresh_token" ); $result = runCurl($postvals, $urlToken, 'POST', $postvals); $jsonToken = json_decode($result); $error = $jsonToken-{'error'}; if ($error) { return false; } else { return true; } }
Java Example
This example uses the library codehaus.jackson:
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-lgpl</artifactId> <version>1.9.13</version> </dependency>
import java.io.OutputStream; import java.net.URL; import java.net.URLEncoder; import java.util.Map; import javax.net.ssl.HttpsURLConnection; import org.codehaus.jackson.map.ObjectMapper; import org.junit.Test; import org.junit.Assert.*; /** * Test for refresh oauth token */ public class AppTest { @Test public void refreshTokenTest() throws Exception { String client_id = "myClientId"; String client_secret = "myClientSecret"; String refresh_token = "xxxxxxxxxx"; String scope = "full"; String token_endpoint_url = "https://be-mn1.mag-news.it/be/oauth/token"; String grant_Type = "refresh_token"; refresh_access_token(client_id, client_secret, refresh_token, scope, token_endpoint_url, grant_Type); } public static void refresh_access_token(String client_id, String client_secret, String refresh_token, String scope, String token_endpoint_url, String grant_type) throws Exception { String postbody = "grant_type=" + grant_type + "" + "&client_id=" + URLEncoder.encode(client_id, "utf-8") + "&scope=" + URLEncoder.encode(scope, "utf-8") + "&refresh_token=" + URLEncoder.encode(refresh_token, "utf-8") + "&client_secret=" + URLEncoder.encode(client_secret, "utf-8"); URL url = new URL(token_endpoint_url); HttpsURLConnection con = null; try { con = (HttpsURLConnection) url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestMethod("POST"); try (OutputStream ost = con.getOutputStream();) { ost.write(postbody.getBytes("utf-8")); } ObjectMapper mapper = new ObjectMapper(); Map result = mapper.readValue(con.getInputStream(), Map.class); String error = (String) result.get("error"); if (error != null) { throw new Exception("error while using refresh token: " + result); } } finally { if (con != null){ con.disconnect(); } } } }