Firebase user authentication for java application (Not Android) - java

I want to create a user authentication form for my java application using firebase. The dependencies for connection to the realtime database is available and the use of Firebase Admin is well documented here.
But presently Firebase Admin supports user authentication only for Node.js and it is documented here.
Here is my test code.
public class Login {
private JPanel jPanel;
public static void main(String[] args) {
// Show My Form
JFrame jFrame = new JFrame("Login");
jFrame.setContentPane(new Login().jPanel);
jFrame.pack();
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
// Firebase
FirebaseOptions options = null;
try {
options = new FirebaseOptions.Builder()
.setServiceAccount(new FileInputStream("xxx.json"))
.setDatabaseUrl("https://xxx.firebaseio.com/")
.build();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (options != null) {
FirebaseApp.initializeApp(options);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Clip");
ref.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("ClipText = [" + dataSnapshot.getValue() + "]");
}
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}
Can anyone please guide me how can I create user authentication (e.g. Create user using email & password, Sign in) for my java application?
Note: I'm using Gradle.

Basing on the REST API documentation here: https://firebase.google.com/docs/reference/rest/auth/
i created a singleton like that to get a valid token from email and password and to validate it using the getAccountInfo method:
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class FireBaseAuth {
private static final String BASE_URL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/";
private static final String OPERATION_AUTH = "verifyPassword";
private static final String OPERATION_REFRESH_TOKEN = "token";
private static final String OPERATION_ACCOUNT_INFO = "getAccountInfo";
private String firebaseKey;
private static FireBaseAuth instance = null;
protected FireBaseAuth() {
firebaseKey = "YOUR FIREBASE KEY HERE";
}
public static FireBaseAuth getInstance() {
if(instance == null) {
instance = new FireBaseAuth();
}
return instance;
}
public String auth(String username, String password) throws Exception {
HttpURLConnection urlRequest = null;
String token = null;
try {
URL url = new URL(BASE_URL+OPERATION_AUTH+"?key="+firebaseKey);
urlRequest = (HttpURLConnection) url.openConnection();
urlRequest.setDoOutput(true);
urlRequest.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStream os = urlRequest.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write("{\"email\":\""+username+"\",\"password\":\""+password+"\",\"returnSecureToken\":true}");
osw.flush();
osw.close();
urlRequest.connect();
JsonParser jp = new JsonParser(); //from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream) urlRequest.getContent())); //Convert the input stream to a json element
JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object.
token = rootobj.get("idToken").getAsString();
} catch (Exception e) {
return null;
} finally {
urlRequest.disconnect();
}
return token;
}
public String getAccountInfo(String token) throws Exception {
HttpURLConnection urlRequest = null;
String email = null;
try {
URL url = new URL(BASE_URL+OPERATION_ACCOUNT_INFO+"?key="+firebaseKey);
urlRequest = (HttpURLConnection) url.openConnection();
urlRequest.setDoOutput(true);
urlRequest.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStream os = urlRequest.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write("{\"idToken\":\""+token+"\"}");
osw.flush();
osw.close();
urlRequest.connect();
JsonParser jp = new JsonParser(); //from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream) urlRequest.getContent())); //Convert the input stream to a json element
JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object.
email = rootobj.get("users").getAsJsonArray().get(0).getAsJsonObject().get("email").getAsString();
} catch (Exception e) {
return null;
} finally {
urlRequest.disconnect();
}
return email;
}
}
This not allow you to create users dinamically but suppose to have them already created on Firebase

Related

Is there a way to put a kind of identificator to asychronous requests?

I have a server and a client that sends requests. I don't need to identify the requests when it works in synch mode. But in asynch mode each request must have an identificator, so I could be sure that the response corresponds to exact request. The server is not to be updated, I have to put the identificator in the client's code. Is there a way do do it? I can't find out any.
Here is my main class. I guess all must be clear, the class is very simple.
public class MainAPITest {
private static int userId = 0;
private final static int NUM_OF_THREADS = 10;
private final static int NUM_OF_USERS = 1000;
public static void main(String[] args) {
Security.addProvider(new GammaTechProvider());
ExecutorService threadsExecutor = Executors.newFixedThreadPool(NUM_OF_THREADS);
for (int i = 0; i < NUM_OF_USERS; i++) {
MyRunnable user = new MyRunnable();
userId++;
user.uId = userId;
threadsExecutor.execute(user);
}
threadsExecutor.shutdown();
}
}
class MyRunnable implements Runnable {
int uId;
#Override
public void run() {
try {
abstrUser user = new abstrUser();
user.setUserId(uId);
user.registerUser();
user.chooseAndImplementTests();
user.revokeUser();
} catch (Exception e) {
e.printStackTrace();
}
}
}
User class describes the user's behaviour. It is long enough, idk if it is needed here. User runs a number of random tests. Each test has its own class that extends abstract test class, where the http connection is established:
import org.json.JSONObject;
import javax.xml.bind.DatatypeConverter;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public abstract class abstractRESTAPITest {
protected String apiUrl;
public abstractRESTAPITest() {
}
protected byte[] sendRequest(JSONObject jsonObject) throws IOException {
return this.sendRequest(jsonObject, (String) null, (String) null);
}
protected byte[] sendRequest(JSONObject jsonObject, String username, String password) throws IOException {
HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL(this.apiUrl)).openConnection();
if (username != null && password != null) {
String userPassword = username + ":" + password;
httpURLConnection.setRequestProperty("Authorization", "Basic " + DatatypeConverter.printBase64Binary(userPassword.getBytes()));
}
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setDoOutput(true);
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.write(jsonObject.toString().getBytes());
dataOutputStream.flush();
System.out.println("REST send: " + jsonObject.toString());
if (httpURLConnection.getResponseCode() != 200) {
System.out.println("REST send error, http code " + httpURLConnection.getResponseCode() + " " + httpURLConnection.getResponseMessage());
throw new IOException();
} else {
byte[] responseBody = null;
StringBuilder data = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
data.append(line);
responseBody = data.toString().getBytes();
}
if (br != null) {
br.close();
}
return responseBody;
}
}
public abstract boolean test();
}
Now I am trying to transform the httpUrlConnection part of the code into a kind of this.
Jsonb jsonb = JsonbBuilder.create();
var client = HttpClient.newHttpClient();
var httpRequest = HttpRequest.newBuilder()
.uri(new URI(apiUrl))
.version(HttpClient.Version.HTTP_2)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonb.toJson(jsonObject)))
.build();
client.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString());
It must send a JSON request and receive JSON response. HttpClient, introduced in Java11, has sendAsync native method, so I try to use it. But I don't understand fully how it works, so I have no success.

Doubts to filter JSON objects from REST API

I am trying to filter some objects from a JSON response in Java. Below is my code. I need to get the Genre Object from the response and print it separately. Anyone who knows how that can be done?
I have did the RestAPI call from omdb. This is a just a simple project which I am trying to build. To basically analyze the type of genres that were released in particular years.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import sun.org.mozilla.javascript.internal.json.JsonParser;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class OmdbApiService {
//public static final String Search_Url = "http://www.omdbapi.com/?s=TITLE&apikey=APIKEY";
//public static final String Search_Url = "http://www.omdbapi.com/?t=TITLE&plot=PLOT&apikey=APIKEY";
public static final String Search_Plot = "http://www.omdbapi.com/?i=TITLE&plot=PLOT&apikey=APIKEY";
private static String sendGetRequest(String requestURL){
StringBuffer response = new StringBuffer();
try {
URL url = new URL(requestURL);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
InputStream stream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
String line;
while ((line = buffer.readLine()) != null) {
response.append(line);
}
buffer.close();
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response.toString();
}
private static String searchMoviebyID(String title, String plot, String key) {
try {
title = URLEncoder.encode(title, "UTF-8"); // To omit the spaces in between the titles
plot = URLEncoder.encode(plot, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String requestUrl = Search_Plot
.replaceAll("TITLE", title)
.replaceAll("PLOT", plot)
.replaceAll("APIKEY", key);
return sendGetRequest(requestUrl);
}
/*private static String filterbyGenres(){
try {
}
}*/
public static void main(String[] args) {
String jsonResponse = OmdbApiService.searchMoviebyID("tt1345836","full","6d****87");
System.out.println(jsonResponse);
/*Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = new JsonParser().parse(jsonResponse);
System.out.println(gson.toJson(jsonResponse));*/
}
}
Output:
{"Title":"The Dark Knight Rises","Year":"2012","Rated":"PG-13","Released":"20 Jul 2012","Runtime":"164 min","Genre":"Action, Thriller","Director":"Christopher Nolan","Writer":"Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)","Actors":"Christian Bale, Gary Oldman, Tom Hardy, Joseph Gordon-Levitt","Plot":"Despite his tarnished reputation after the events of The Dark Knight, in which he took the rap for Dent's crimes, Batman feels compelled to intervene to assist the city and its police force which is struggling to cope with Bane's plans to destroy the city.","Language":"English, Arabic","Country":"UK, USA","Awards":"Nominated for 1 BAFTA Film Award. Another 38 wins & 102 nominations.","Poster":"https://m.media-amazon.com/images/M/MV5BMTk4ODQzNDY3Ml5BMl5BanBnXkFtZTcwODA0NTM4Nw##._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"8.4/10"},{"Source":"Rotten Tomatoes","Value":"87%"},{"Source":"Metacritic","Value":"78/100"}],"Metascore":"78","imdbRating":"8.4","imdbVotes":"1,372,667","imdbID":"tt1345836","Type":"movie","DVD":"03 Dec 2012","BoxOffice":"$448,130,642","Production":"Warner Bros. Pictures","Website":"http://www.thedarkknightrises.com/","Response":"True"}
This is the output, can I know how do I filter out just the Genre in this output.
Extra help : If someone could tell me how to print the output in separate lines, it will be helpful.
You can parse it using the jackson library. Can you try this code?
Jackson:
// jackson library import
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
// ...
private static String filterByGenres(String jsonResponse) {
String genres = "";
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readValue(jsonResponse, JsonNode.class);
// Considering when there are no API results
if(jsonNode != null || jsonNode.get("Genre") != null) {
genres = jsonNode.get("Genre").asText();
}
} catch (Exception e) {
// handle to exception
}
return genres;
}
public static void main(String[] args) {
String jsonResponse = OmdbApiService.searchMoviebyID("tt1345836", "full", "6d****87");
// The result of the API is the argument.(json format string)
String genres = filterByGenres(jsonResponse);
System.out.println(genres); // Action, Thriller
}
Gson:
public static void main(String[] args) {
String jsonResponse = OmdbApiService.searchMoviebyID("tt1345836", "full", "6d****87");
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(jsonResponse).getAsJsonObject();
JsonElement genreObject = jsonObject.get("Genre");
System.out.println(genreObject.getAsString()); // Action, Thriller
}
Extra help:
Extra help : If someone could tell me how to print the output in separate lines, it will be helpful.
public void prettyPrint() {
String jsonResponse = OmdbApiService.searchMoviebyID("tt1345836", "full", "6d****87");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = new JsonParser().parse(jsonResponse);
String prettyJson = gson.toJson(jsonElement);
System.out.println(prettyJson);
}

JSON extraction author guardian API

So, I'm trying to extract JSON from the Guardian newspaper API.
Basically I can get everything except the author which is crucial.
How do or what is a different way of extracting this.
Many thanks in advance I'm new to all this and have asked questions i n the past to no avail any advice would be greatly appreciated.
QueryUtils.Java
package com.example.android.newsapp;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class QueryUtils {
private static final String TAG = QueryUtils.class.getSimpleName();
public static Context context;
private QueryUtils() {
}
public static List<News> fetchNews(String requestUrl) {
URL url = createUrl(requestUrl);
String json_response = null;
try {
json_response = makeHttpRequest(url);
Log.i(TAG, json_response);
} catch (IOException e) {
e.printStackTrace();
}
List<News> news = extractFromJson(json_response);
return news;
}
private static URL createUrl(String StringUrl) {
URL url = null;
try {
url = new URL(StringUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return url;
}
private static String makeHttpRequest(URL url) throws IOException {
String json_response = "";
if (url == null) {
return json_response;
}
HttpURLConnection httpURLConnection = null;
InputStream inputStream = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() == 200) {
inputStream = httpURLConnection.getInputStream();
json_response = readFromString(inputStream);
} else {
Log.e(TAG, "Error" + httpURLConnection.getResponseCode());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return json_response;
}
private static String readFromString(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
} private static String extractString(JSONObject newInfo, String stringName) {
String str = null;
try {
str = newInfo.getString(stringName);
} catch (JSONException e) {
Log.e(TAG, context.getString(R.string.query_util_error_extract_string) + stringName);
}
if (str != null) {
return str;
} else {
return context.getString(R.string.empty_string);
}
}
private static List<News> extractFromJson(String news_json) {
if (TextUtils.isEmpty(news_json)) {
return null;
}
List<News> news = new ArrayList<News>();
try {
JSONObject baseJson = new JSONObject(news_json);
JSONArray news_array = baseJson.getJSONObject("response").getJSONArray("results");
for (int i = 0; i < news_array.length(); i++) {
JSONObject currentNews = news_array.getJSONObject(i);
String name = currentNews.getString("sectionName");
String title = currentNews.getString("webTitle");
String date = currentNews.getString("webPublicationDate");
String url = currentNews.getString("webUrl");
JSONArray tags = baseJson.getJSONArray("tags");
String contributor = null;
if (tags.length() == 1) {
JSONObject contributorTag = (JSONObject) tags.get(0);
contributor = extractString(contributorTag, context.getString(R.string.query_util_json_web_title));
} else {
//no contributor
contributor = context.getString(R.string.empty_string);
}
News mNews = new News(name, title, date, url, contributor);
news.add(mNews);
}
} catch (JSONException e) {
e.printStackTrace();
}
return news;
}
}
This is the JSON that I'm extracting from.
https://content.guardianapis.com/search?q=debate&tag=politics/politics&from-date=2014-01-01&api-key=test
This is the Data-Provider..
http://open-platform.theguardian.com/documentation/
I too had trouble getting the author. There were two changes I made that resolved the issue.
First, I did have to change the add the &show-tags=contributor to the url.
Second, I had to tweak your your if statement in parsing to read. Instead of :
contributor = extractString(contributorTag, context.getString(R.string.query_util_json_web_title));
I replaced with :
contributor = contributorTag.getString("webTitle");
(The key "webTitle" contains the author's name)
A problem for you is the url you used doesn't give the tag array which contains the webTitle key, even after I adjusted it with the &show-tags=contributor.
The url I used is:
http://content.guardianapis.com/search?&show-tags=contributor&q=%27tech%27&api-key=2bbbc59c-5b48-46a5-83d3-8435d3136348
The full QueryUtils.java file is:
package com.example.android.technewsapps1;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public final class QueryUtils {
private static final String LOG_TAG = QueryUtils.class.getSimpleName();
private QueryUtils() {
}
/**
* Query the Guardian dataset and return a list of NewsStory objects.
*/
public static List<NewsStory> fetchNewsStoryData(String requestUrl) {
// Create URL object
URL url = createUrl(requestUrl);
// Perform HTTP request to the URL and receive a JSON response back
String jsonResponse = null;
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
Log.e(LOG_TAG, "Problem making the HTTP request.", e);
}
// Extract relevant fields from the JSON response and create a list of NewsStories
List<NewsStory> newsStories = extractFeatureFromJson(jsonResponse);
// Return the list of NewsStories
return newsStories;
}
/**
* Returns new URL object from the given String URL.
*/
private static URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Problem building the URL ", e);
}
return url;
}
/**
* Make an HTTP request to the given URL and return a String as the response.
*/
private static String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
// If the URL is null, then return early.
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the newsStory JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
// Closing the input stream could throw an IOException, which is why
// the makeHttpRequest(URL url) method signature specifies than an IOException
// could be thrown.
inputStream.close();
}
}
return jsonResponse;
}
/**
* Convert the {#link InputStream} into a String which contains the
* whole JSON response from the server.
*/
private static String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
/**
* Return a list of NewsStory objects that has been built up from
* parsing the given JSON response.
*/
private static List<NewsStory> extractFeatureFromJson(String newsStoryJSON) {
// If the JSON string is empty or null, then return early.
if (TextUtils.isEmpty(newsStoryJSON)) {
return null;
}
// Create an empty ArrayList that we can start adding news stories to
List<NewsStory> newsStories = new ArrayList<>();
// Try to parse the JSON response string. If there's a problem with the way the JSON
// is formatted, a JSONException exception object will be thrown.
// Catch the exception so the app doesn't crash, and print the error message to the logs.
try {
// Create a JSONObject from the JSON response string
JSONObject baseJsonResponse = new JSONObject(newsStoryJSON);
//Create the JSONObject with the key "response"
JSONObject responseJSONObject = baseJsonResponse.getJSONObject("response");
//JSONObject responseJSONObject = baseJsonResponse.getJSONObject("response");
// Extract the JSONArray associated with the key called "results",
// which represents a list of news stories.
JSONArray newsStoryArray = responseJSONObject.getJSONArray("results");
// For each newsStory in the newsStoryArray, create an NewsStory object
for (int i = 0; i < newsStoryArray.length(); i++) {
// Get a single newsStory at position i within the list of news stories
JSONObject currentStory = newsStoryArray.getJSONObject(i);
// Extract the value for the key called "webTitle"
String title = currentStory.getString("webTitle");
// Extract the value for the key called "sectionName"
String sectionName = currentStory.getString("sectionName");
// Extract the value for the key called "webPublicationDate"
String date = currentStory.getString("webPublicationDate");
// Extract the value for the key called "url"
String url = currentStory.getString("webUrl");
//Extract the JSONArray with the key "tag"
JSONArray tagsArray = currentStory.getJSONArray("tags");
//Declare String variable to hold author name
String authorName = null;
if (tagsArray.length() == 1) {
JSONObject contributorTag = (JSONObject) tagsArray.get(0);
authorName = contributorTag.getString("webTitle");
}
// Create a new NewsStory object with the title, section name, date,
// and url from the JSON response.
NewsStory newsStory = new NewsStory(title, sectionName, date, url, authorName);
// Add the new NewsStory to the list of newsStories.
newsStories.add(newsStory);
}
} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing the newsStory JSON results", e);
}
// Return the list of earthquakes
return newsStories;
}
}

GoogleIdTokenVerifier does not return name, picture, etc

We're using the goole-client-api library to verify google accounts from our backend. This implementation runs on Google Cloud Platform (GCP) using App Engine and DataStore.
What we're seeing so far is for the GoogleIdTokenVerifier to work but only returns email and uid along with token signature.
The token and uid used does return all profile infos when run against our servlet but does not through our App Engine Endpoint.
Here is the code used:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.logging.Logger;
public class GoogleVerifier implements TokenVerifier {
final Logger logger = Logger.getLogger(GoogleVerifier.class.getName());
private static GoogleVerifier instance = null;
private String privAppId;
private UrlFetchTransport httpTransport; //library required to run on GCP
private JsonFactory jsonFactory;
private GoogleVerifier() {
}
private static GoogleVerifier getInstance() {
if (instance == null) {
instance = new GoogleVerifier();
}
return instance;
}
public static void setAppId(String appId) {
getInstance().setPrivAppId(appId);
getInstance().setHttpTransport(new UrlFetchTransport());
getInstance().setJsonFactory(new JacksonFactory());
}
public static String[] verify(String token, String uid) {
return getInstance().verifyPrivate(token, uid);
}
public String[] verifyPrivate(String token, String uid) {
#SuppressWarnings("unused")
GoogleCredential credential = new GoogleCredential().setAccessToken(token);
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(httpTransport, jsonFactory)
.setAudience(Collections.singletonList(privAppId))
.build();
String[] payloadInfo = new String[5];
try {
GoogleIdToken idToken = verifier.verify(token);
if (idToken != null) {
GoogleIdToken.Payload payload = idToken.getPayload();
if (payload.getSubject().equals(uid)) {
logger.info("Matching google id: " + uid);
payloadInfo[0] = payload.getSubject();
payloadInfo[1] = payload.get("given_name").toString();
payloadInfo[2] = payload.get("family_name").toString();
payloadInfo[3] = payload.get("picture").toString();
payloadInfo[4] = payload.getEmail();
return payloadInfo;
} else {
logger.info("Mismatching google id: " + uid);
return payloadInfo;
}
}
}
catch (Exception e) {
e.printStackTrace();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
logger.warning(sw.toString());
return payloadInfo;
}
return payloadInfo;
}
private void setPrivAppId(String appId) {
this.privAppId = appId;
}
private void setHttpTransport(UrlFetchTransport httpTransport) {
this.httpTransport = httpTransport;
}
private void setJsonFactory(JsonFactory jsonFactory) {
this.jsonFactory = jsonFactory;
}
}
Here is our App Engine Endpoint:
#ApiMethod(name = "loginSocial", path = "loginSocial", httpMethod = HttpMethod.PUT)
public Response loginSocial(#Named("token") String token,
#Named("uid") String uid,
#Named("socialWebSite") SOCIALWEBSITE socialWebSite,
HttpServletRequest request) throws DatabaseException, IOException {
Response response = new Response();
//make sure parameters and not null or empty
if (token != null && uid != null && socialWebSite != null &&
!token.trim().isEmpty() && !uid.trim().isEmpty()){
String [] userInfo = new String[5];
//validate token and retrieve info first
if (socialWebSite.equals(SOCIALWEBSITE.GOOGLE)){
GoogleVerifier.setAppId(APP_ID);
userInfo = GoogleVerifier.verify(token, uid);
}else if(socialWebSite.equals(APP_ID);
userInfo = FacebookVerifier.verify(token, uid);
}
}
}
Thanks!
I ended up using a different library which is much simpler and provided the same information.
https://stackoverflow.com/questions/22516693

Android: JSON parsing error nothing comes up

I am trying to parse JSON, I have done it there before but not quiet this way. I spent hours trying to solve the problem, but I dont know what is wrong with the code.I am attaching the entire code for the activity, Web Request class and the layout. Any help will be greatly appreciated.
I get this error
java.io.FileNotFoundException: /data/system/users/sdp_engine_list.xml: open failed: ENOENT (No such file or directory)
05-19 18:17:27.427 3450-3450/? W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
05-19 18:17:28.232 3450-3592/? W/DisplayManagerService: Failed to notify process 20004 that displays changed, assuming it died.
This is the activity Transactions class.
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import com.rectuca.iyzicodemo.Classes.Transaction;
import com.rectuca.iyzicodemo.Library.WebRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class Transactions extends ListActivity {
// URL to get contacts JSON
private static String url = "http://www.mocky.io/v2/573dbd243700005f194dcdcc";
// JSON Node names
private static final String TAG_PAYMENTS= "payments";
private static final String TAG_PAYMENT_ID = "paymentId";
private static final String TAG_SENT_BY = "sentBy";
private static final String TAG_DATE_TIME = "dateTime";
private static final String TAG_SENT_TO = "sentTo";
private static final String TAG_BANK_NAME = "bankName";
private static final String TAG_INSTALLMENTS = "installments";
private static final String TAG_AMOUNT = "amount";
private static final String TAG_3DS = "threeDs";
private static final String TAG_CANCELLED = "cancelled";
private static final String TAG_RETURNED = "returned";
private static final String TAG_TRANSACTION_STATUS = "transactionStatus";
private static final String TAG_BLOCKAGE_AMOUNT = "blockage_amount";
private static final String TAG_BLOCKAGE_RELEASE_DATE = "blockageReleaseDate";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transactions);
// Calling async task to get json
new GetInfo().execute();
}
/**
* Async task class to get json by making HTTP call
*/
private class GetInfo extends AsyncTask<Void, Void, Void> {
// Hashmap for ListView
ArrayList<HashMap<String, String>> transactionInfoList;
ProgressDialog proDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress loading dialog
proDialog = new ProgressDialog(Transactions.this);
proDialog.setMessage("Please Wait...");
proDialog.setCancelable(false);
proDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
WebRequest webReq = new WebRequest();
// Making a request to url and getting response
String jsonStr = webReq.makeWebServiceCall(url, WebRequest.GET);
Log.d("Response: ", "> " + jsonStr);
transactionInfoList = ParseJSON(jsonStr);
return null;
}
#Override
protected void onPostExecute(Void requestresult) {
super.onPostExecute(requestresult);
// Dismiss the progress dialog
if (proDialog.isShowing())
proDialog.dismiss();
/**
* Updating received data from JSON into ListView
* */
transactionInfoList=new ArrayList<HashMap<String, String>>();
ListAdapter adapter = new SimpleAdapter(
Transactions.this, transactionInfoList,
R.layout.row_layout, new String[]{TAG_SENT_TO,TAG_DATE_TIME
,TAG_BANK_NAME,TAG_AMOUNT,TAG_3DS, TAG_CANCELLED,
TAG_RETURNED},
new int[]{R.id.name,R.id.dateTime ,R.id.bankName,R.id.amount,
R.id.threeDS, R.id.cancelled, R.id.returned});
setListAdapter(adapter);
}
}
private ArrayList<HashMap<String, String>> ParseJSON(String json) {
if (json != null) {
try {
// Hashmap for ListView
ArrayList<HashMap<String, String>> paymentList = new ArrayList<HashMap<String, String>>();
JSONObject jsonObj = new JSONObject(json);
// Getting JSON Array node
JSONArray payments = jsonObj.getJSONArray(TAG_PAYMENTS);
// looping through All Payments
for (int i = 0; i < payments.length(); i++) {
JSONObject c = payments.getJSONObject(i);
String dateTime =c.getString(TAG_DATE_TIME);
String sentTo =c.getString(TAG_SENT_TO);
String bankName =c.getString(TAG_BANK_NAME)+" ( "+c.getString(TAG_INSTALLMENTS)+" ) " ;
String amount =c.getString(TAG_AMOUNT);
String threeDS =c.getString(TAG_3DS);
String cancelled =c.getString(TAG_CANCELLED);
String returned =c.getString(TAG_RETURNED);
// temporary hashmap for a single payment
HashMap<String, String> payment = new HashMap<String, String>();
// adding every child node to HashMap key => value
payment.put(TAG_DATE_TIME, dateTime);
payment.put(TAG_SENT_TO, sentTo);
payment.put(TAG_BANK_NAME, bankName);
payment.put(TAG_AMOUNT, amount);
payment.put(TAG_3DS, threeDS);
payment.put(TAG_CANCELLED, cancelled);
payment.put(TAG_RETURNED, returned);
// adding student to students list
paymentList.add(payment);
}
return paymentList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
} else {
Log.e("ServiceHandler", "No data received from HTTP Request");
return null;
}
}
}
This is the WebRequest Class
package com.rectuca.iyzicodemo.Library;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class WebRequest {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
//Constructor with no parameter
public WebRequest() {
}
/**
* Making web service call
*
* #url - url to make request
* #requestmethod - http request method
*/
public String makeWebServiceCall(String url, int requestmethod) {
return this.makeWebServiceCall(url, requestmethod, null);
}
/**
* Making service call
*
* #url - url to make request
* #requestmethod - http request method
* #params - http request params
*/
public String makeWebServiceCall(String urladdress, int requestmethod,
HashMap<String, String> params) {
URL url;
String response = "";
try {
url = new URL(urladdress);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setDoInput(true);
conn.setDoOutput(true);
if (requestmethod == POST) {
conn.setRequestMethod("POST");
} else if (requestmethod == GET) {
conn.setRequestMethod("GET");
}
if (params != null) {
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
writer.write(result.toString());
writer.flush();
writer.close();
os.close();
}
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
}
This is what I am trying to do
http://lh3.googleusercontent.com/JqcySZU2Pz067NutlDvPP5Zq_3n_WSAllIuEdjQjOjyeGkKguaMNCrltaKbjBCi16g=h900-rw
I would suggest you to use
VOLLEY networking library by google
You should try to use Volley JSONOBJECTREQUEST() and you won't have any issue after that parsing will be easier.
add this to dependency section of your app
dependencies {
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
}

Categories