Last active
February 18, 2019 00:21
-
-
Save marceloinacio/4f5c8f72a333485e6927551c4e849f9a to your computer and use it in GitHub Desktop.
PubNub Grant Java RestAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.pubnub.api.PubNubException; | |
import com.pubnub.api.PubNubUtil; | |
import javax.net.ssl.HttpsURLConnection; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created by tukunare on 2/2/2017. | |
*/ | |
public class TestGrantRestAPI { | |
public static void main(String[] args) { | |
try { | |
String publishKey=""; | |
String subscribeKey = ""; | |
String secretKey=""; | |
String authKey="myAuth"; | |
String signature = ""; | |
int timestamp = (int) ((new Date().getTime()) / 1000); | |
Map<String, String> queryParams = new HashMap<>(); | |
queryParams.put("channel", "restChannel"); | |
queryParams.put("auth", authKey); | |
queryParams.put("r", "1"); | |
queryParams.put("m", "1"); | |
queryParams.put("timestamp", String.valueOf(timestamp)); | |
String signInput = subscribeKey + "\n" | |
+ publishKey + "\n" | |
+ "grant" + "\n"; | |
signInput += PubNubUtil.preparePamArguments(queryParams); | |
try { | |
signature = PubNubUtil.signSHA256(secretKey, signInput); | |
} catch (PubNubException e) { | |
System.out.println("signature failed on SignatureInterceptor: " + e.toString()); | |
} | |
String query=""; | |
for(String key : queryParams.keySet()) { | |
query += key + "=" + queryParams.get(key) + "&"; | |
} | |
String rest = "https://ps.pndsn.com/v1/auth/grant/sub-key/" + subscribeKey + "?" + query + "signature=" + signature; | |
URL url; | |
try { | |
url = new URL(rest); | |
HttpsURLConnection con = (HttpsURLConnection)url.openConnection(); | |
//dump all the content | |
print_content(con); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
static void print_content(HttpsURLConnection con) { | |
if (con != null) { | |
try { | |
System.out.println("****** Content of the URL ********"); | |
BufferedReader br = | |
new BufferedReader( | |
new InputStreamReader(con.getInputStream())); | |
String input; | |
while ((input = br.readLine()) != null) { | |
System.out.println(input); | |
} | |
br.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment