Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.savvasdalkitsis.jtmdb;
  
  import java.net.URL;
  
  
  import  org.json.JSONException;
  import  org.json.JSONObject;
 
Class that handles authorization with tmdb. The authentication procedure is as follows. The user must request a token from the server using the method getToken(). Then using the method authorizeToken(String) the user must browse to the returned URL and authenticate the token. Once the token is authenticated, the user can create an authenticated session using the method getSession(String)

Author(s):
Savvas Dalkitsis
 
 public class Auth {
 
 	private Auth() {
 
 	}

Gets the session for the authenticated token. Will return a pair of Session and the ServerResponse or null if there was an error with the API or an IOException occurred.

Parameters:
token The authenticated token.
Returns:
A pair of Session and the ServerResponse or null if there was an error with the API or an IOException occurred.
 
 	public static Pair<SessionServerResponsegetSession(String token) {
 		Log.log("Getting session for token " + token.);
 		if (GeneralSettings.getApiKey() != null
 				&& !GeneralSettings.getApiKey().equals("")) {
 			try {
 				URL call = new URL(.
 						+ GeneralSettings.getApiKey() + "/" + token);
 				String jsonString = Utilities.readUrlResponse(call);
 				try {
 					JSONObject jsonObject = new JSONObject(jsonString
 							.toString());
 					if (jsonObject.has("code")) {
 						return new Pair<SessionServerResponse>(null,
 								ServerResponse.forID(jsonObject.getInt("code")));
 					}
 					String userName = jsonObject.getString("username");
 					String session = jsonObject.getString("session");
 					return new Pair<SessionServerResponse>(new Session(
 							userNamesession), .);
 				} catch (JSONException e) {
 					Log.log(e.);
 					return new Pair<SessionServerResponse>(null,
 				}
 			} catch (IOException e) {
 				Log.log(e.);
 			}
 		} else {
 			Log.log("Error with the API key".);
 		}
 		return null;
 	}

Will return the URL that the user must browse to, in order to authenticate the provided token.

Parameters:
token The token to authenticate.
Returns:
The URL that the user must browse to, in order to authenticate the provided token.
 
 	public static URL authorizeToken(String token) {
 		try {
 			return new URL("http://www.themoviedb.org/auth/" + token);
 		} catch (MalformedURLException e) {
 			Log.log(e.);
 		}
 		return null;
 	}

Requests a token from the server that can be used to create an authenticated session. Will be null if an error occurred.

Returns:
A token from the server that can be used to create an authenticated session. Will be null if an error occurred.
Throws:
IOException
JSONException
	public static String getToken() throws IOException, JSONException {
		Log.log("Getting token for authentication".);
		if (GeneralSettings.getApiKey() != null
				&& !GeneralSettings.getApiKey().equals("")) {
			try {
						+ GeneralSettings.getApiKey());
				String jsonString = Utilities.readUrlResponse(call).trim();
				if ((jsonString.startsWith("[") || jsonString.startsWith("{"))
						&& !jsonString.equals("[\"Nothing found.\"]")) {
					JSONObject jsonObject = new JSONObject(jsonString
					String token = jsonObject.getString("token");
					return token;
else {
					Log
									"Getting token for authentication returned no results",
				}
catch (IOException e) {
				throw e;
catch (JSONException e) {
				throw e;
			}
else {
			Log.log("Error with the API key".);
		}
		return null;
	}
New to GrepCode? Check out our FAQ X