Using OAuth with Scribe on Android - java

Hi I'm using the scribe library for OAuth communication with LInkedIn.
I have a login class and a query class.
The login class creates a service to get my request token and secret and creates my access token. The access token is then saved to the preferences file. This seems to work fine and I can make a successful api call after all the tokens have been created.
In my OAuth query class i retrieve the access token, create another service and try to make an API call, but when ever I load an activity which makes calls this class it causes my app to crash. I have tested to make sure that the access token is saved and they are.
Here is my login class
public class Login_LinkedIn extends Activity
{
SharedPreferences settings;
OAuthService service;
Token requestToken;
Button home;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.webauth);
initControls();
service = new ServiceBuilder()
.provider(LinkedInApi.class)
.apiKey( getString(R.string.apiKey) )
.apiSecret( getString(R.string.secKey) )
.callback( getString(R.string.callBack) )
.build();
requestToken = service.getRequestToken();
final String authURL = service.getAuthorizationUrl(requestToken);
final WebView webview = (WebView) findViewById(R.id.webView);
//attach WebViewClient to intercept the callback url
webview.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
//check for our custom callback protocol
//otherwise use default behavior
if(url.startsWith( getString(R.string.callBack) ))
{
//authorization complete hide webview for now.
webview.setVisibility(View.GONE);
Uri uri = Uri.parse(url);
String verifier = uri.getQueryParameter("oauth_verifier");
Verifier v = new Verifier(verifier);
//save this token for practical use.
Token accessToken = service.getAccessToken(requestToken, v);
OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.linkedin.com/v1/people/~:(first-name,last-name)");
service.signRequest(accessToken, request);
Response response = request.send();
xmlHandler xh = new xmlHandler(response.getBody());
settings = getSharedPreferences("preferences", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("accessToken", accessToken.getToken());
// The requestToken is saved for use later on to verify the OAuth request.
// See onResume() below
editor.putString("requestToken", requestToken.getToken());
editor.putString("requestSecret", requestToken.getSecret());
editor.putString("first-name", xh.getValue("first-name"));
editor.putString("last-name", xh.getValue("last-name"));
editor.commit();
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
//send user to authorization page
webview.loadUrl(authURL);
}
#Override
protected void onResume()
{
super.onResume();
Intent i = getIntent();
if(i != null)
{
Uri uri = i.getData();
if(uri != null)
{
String oauthVerifier = uri.getQueryParameter("oauth_verifier");
Verifier verifier = new Verifier(oauthVerifier);
requestToken = new Token(settings.getString("requestToken", null), settings.getString("requestSecret", null));
Token accessToken = service.getAccessToken(requestToken, verifier);
// Save the access token.
SharedPreferences.Editor editor = settings.edit();
editor.remove("requestToken");
editor.remove("requestSecret");
editor.putString("accessToken", accessToken.getToken());
editor.putString("accessSecret", accessToken.getSecret());
editor.commit();
// Start the film list activity.
final Intent intent = new Intent(this,ProConnect.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}
private void initControls()
{
home = (Button)findViewById(R.id.home);
final Intent intent = new Intent(this,ProConnect.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
home.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{
startActivity(intent);
}
});
}
}
and my query class
public class oAuthQuery extends Activity
{
OAuthService service;
Token accessToken;
SharedPreferences settings;
public oAuthQuery()
{
service= new ServiceBuilder()
.provider(LinkedInApi.class)
.apiKey( getString(R.string.apiKey) )
.apiSecret( getString(R.string.secKey) )
.callback( getString(R.string.callBack) )
.build();
settings = getSharedPreferences("preferences", 0);
accessToken = new Token(settings.getString("accessToken", null), settings.getString("accessSecret", null));
}
public String query(String s)
{
OAuthRequest request = new OAuthRequest(Verb.GET, s);
service.signRequest(accessToken, request);
Response response = request.send();
return response.getBody();
}
public OAuthService getService()
{
return service;
}
}
Thanks for any help
Jeff

I solved the problem it was something stupid in the end. I had edited my code to save the access secret and the the access token but had forgotten to re-login when testing my app on my phone. This resulted in the code which saved the parts of the token was not being reached.
I still have a problem using the invite call in the LinkedIn API
invite.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{
inviteXml = inviteCreator.inviteString(to, subj, body, authName, authValue);
nameField.setText(inviteXml);
titleField.setText("");
call = "http://api.linkedin.com/v1/people/~/mailbox";
request = new OAuthRequest(Verb.GET, call);
request.addPayload(inviteXml);
service.signRequest(accessToken, request);
response = request.send();
invite.setVisibility(View.GONE);
}
});
I'm not sure if this is the correct way to add the XML string to the call. The LinkedIn API doesn't specify how it is to be added. Has anyone any experience with this?

Related

Correctly check if user is logged in. Android

I have my login setup like this, I use retrofit to authenticate from the server and I'm using a token api authentication:
In the MainActivity.java method onCreate
apiInterface = ApiClient.getClient().create(ApiInterface.class);
User user = SharedPreferencesHelper.getUser(MainActivity.this);
if (user.getToken() == null) {
Intent login = new Intent(MainActivity.this, LoginActivity.class);
startActivity(login);
} else {
setContentView(R.layout.activity_main);
buildMain();
}
In the LoginActivity.java method on create (I'll summarize, the code is quite long)
loginbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//get login ingo
Login login = new Login(scardI, passwordI, device_name);
Call<User> call = apiInterface.LoginUser(login);
call.enqueue(new Callback<User>() {
#Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
User user = response.body();
//save user info into SharedPreferences
SharedPreferencesHelper.setUser(LoginActivity.this, user);
//check if user saved correctly by getting the user token
if (SharedPreferencesHelper.getUserToken(LoginActivity.this) != null) {
finish();
}
} else {
//show error message
}
}
#Override
public void onFailure(Call<User> call, Throwable t) {
//show error message
}
});
}
});
I'm having an issue, after the user logs in a blank page is displayed instead of the activity_main.xml however when I close the app and reopen it it takes me straight to the activity_main.xml as expected. Is there a reason that after the login it doesn't take me to the activity_main like expected.
An intent to MainActivity.java is missing on success. A blank screen is displayed because finish() is called.
if (response.isSuccessful()) {
User user = response.body();
SharedPreferencesHelper.setUser(LoginActivity.this, user);
if (SharedPreferencesHelper.getUserToken(LoginActivity.this) != null){
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(i);
}
}

New activity not keeping facebook access token

Basically I'm using the facebook sdk in my android app. However, on my scoreboard activity i need to use the access token to get my results for my high scores.
However everytime I try to use
accessToken = AccessToken.getCurrentAccessToken();
It's either null or my app crashes because it's a null pointer error.
java.lang.RuntimeException: Unable to start activity ComponentInfo{wmrapplications.plankoff/wmrapplications.plankoff.ScoreBoard}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.AccessToken.getToken()' on a null object reference
What I need it to do is retrieve the current login for the current login session.
I tried this using an "isloggedin" method.
public boolean isLoggedIn() {
accessToken = AccessToken.getCurrentAccessToken();
if (accessToken == null) {
Toast.makeText(getApplicationContext(), "no access token", Toast.LENGTH_LONG).show();
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
// fetchUserInfo();
}
};
callbackManager = CallbackManager.Factory.create();
} else {
Toast.makeText(getApplicationContext(), " access token", Toast.LENGTH_LONG).show();
Token = accessToken.getToken().toString();
}
return accessToken != null;
}
along with a fetchinfo function
private void fetchUserInfo() {
accessToken = AccessToken.getCurrentAccessToken();
Toast.makeText(getApplicationContext(),
"Relogin successful?" + AccessToken.getCurrentAccessToken().getToken().toString(), Toast.LENGTH_LONG)
.show();
if (accessToken != null) {
GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject me, GraphResponse response) {
// LinkFacebook(socialService);
// FindFriends(socialService);
Token = accessToken.getToken().toString();
}
});
Bundle parameters = new Bundle();
parameters.putString(FIELDS, REQUEST_FIELDS);
request.setParameters(parameters);
GraphRequest.executeBatchAsync(request);
} else {
Toast.makeText(getApplicationContext(), "Relogin failed.", Toast.LENGTH_LONG).show();
}
}
However my accesstoken is still null.
Thanks
What I ended up doing was saving the token in sharedpreferences, then using it later in my app. That allowed me to use my tokens in all my activities.
Whether or not it will break later on down the line I'm not sure.

Android Account Manager and starting another activity on successful login

I'm currently developing my app and I created simple authentication process. When user give login and password it sends data to server and it checks MySql database. I want to store user credentials in safe place which we know is Account Manager. I cant figure out how it works and how to apply it into my code.
Many tutorials that I found are old like 2010 or older..
This code works fine but I need to add changes. Like if user is succesfully logged in I want to start activity which will redirect him to MainPanel.class activity. I've tried to put code like this to SingnInActivity but it says method startActivity is not recognizable. Any ideas how to make it work?
public void login(View view){
Intent intent = new Intent(this, loginActivity.class);
startActivity(intent);
}
Can anybody help me? I appreciate any help.
My loginActivity looks like this:
public class loginActivity extends Activity {
private EditText usernameField,passwordField;
private TextView status,role;
public String d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText)findViewById(R.id.usernameField);
passwordField = (EditText)findViewById(R.id.passwordField);
status = (TextView)findViewById(R.id.status);
role = (TextView)findViewById(R.id.textView5);
}
public void loginPost(View view){
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
//there I use another activity to 'sign in'
new SignInActivity(this,status,role).execute(username,password);
}
}
This is SignInActivity
public class SignInActivity extends AsyncTask<String,Void,String>{
private TextView statusField,roleField;
private Context context;
String d;
public SignInActivity(Context context, TextView statusField, TextView roleField) {
this.context = context;
this.statusField = statusField;
this.roleField = roleField;
}
protected void onPreExecute(){
}
#Override
protected String doInBackground(String... arg0) {
try{
String username = (String)arg0[0];
String password = (String)arg0[1];
String link="http://myserver/index.php";
String data = URLEncoder.encode("username", "UTF-8")
+ "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8")
+ "=" + URLEncoder.encode(password, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter
(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader
(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
d=sb.toString();
return d;
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
protected void onPostExecute(String result){
if(result.equals("adminstrator")){
this.statusField.setText("Yes");
//if user is in my database it changes statusfield to 'yes'
}
else{
this.statusField.setText("No");
}
this.roleField.setText(result);
}
}
There is no method startActivity in AsyncTask. It is in Activity class. If you don't understand what Inheritance is - start with that link.
Change Context to Activity:
private Activity context;
public SignInActivity(Activity context, TextView statusField, TextView roleField) {
this.context = context;
this.statusField = statusField;
this.roleField = roleField;
}
Then use
Intent intent = new Intent(this, MainPanel.class);
context.startActivity(intent);
Using the AccountManager is, actually, pretty complicated. You really just cannot drop it into an existing workflow. You'll need an implementation of the AbstractAccountAuthenticator that can be bound by the Android framework. It will call your login activity as necessary.
There is a fairly understandable example in this app
... and, incidentally, a good description of how it all works in my book, Enterprise Android

Google Cloud Messaging - Check if device is already registered

I just set up GCM in my Android App. But I have the problem that I don't know how to check if the device is already registered. I work with the new google play services library.
The register part looks like this:
#Override
protected String doInBackground(String... arg0) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context_app);
}
regid = gcm.register(SENDER_ID);
msg = "Dvice registered, registration ID=" + regid;
Log.d("111", msg);
sendRegistrationIdToBackend(regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
How can I modify this that it checks if the device is already registered?
Store the registration id in a databade table or shared preference and when app starting..check whether it is null or not
Google has provided very clear documentation with code.You should use following code:
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
registerReceiver(mHandleMessageReceiver,
new IntentFilter(DISPLAY_MESSAGE_ACTION));
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
// Automatically registers application on startup.
GCMRegistrar.register(this, SENDER_ID);
} else {
// Device is already registered on GCM, check server.
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
mDisplay.append(getString(R.string.already_registered) + "\n");
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
boolean registered =
ServerUtilities.register(context, regId);
// At this point all attempts to register with the app
// server failed, so we need to unregister the device
// from GCM - the app will try to register again when
// it is restarted. Note that GCM will send an
// unregistered callback upon completion, but
// GCMIntentService.onUnregistered() will ignore it.
if (!registered) {
GCMRegistrar.unregister(context);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
#Override
protected void onDestroy() {
if (mRegisterTask != null) {
mRegisterTask.cancel(true);
}
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(this);
super.onDestroy();
}
private final BroadcastReceiver mHandleMessageReceiver =
new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
mDisplay.append(newMessage + "\n");
}
};
when you get registration Id, Store it in SharedPreferences, for example:
SharedPreferences shp = context.getSharedPreferences("anyNameYouLike",MODE_PRIVATE);
SharedPreferences.Editor editor=shp.edit();
editor.putString("RegID",registrationID).commit;
In the next time before you register check the "anyNameYouLike" if it contain field called RegID Like this:
private boolean isRegistered(Context context){
SharedPreferences shp = context.getSharedPreferences("anyNameYouLike",PRIVATE_MODE);
return shp.contains("RegID");
}

Getting a Malformed access token "t ​ype":"OAuthException","code":190

I am writing an android application to get the Facebook user albums and photos and display in my Android application.
I have created a Facebook App with APP_ID 281846961912565.
While creating the Facebook instance, I am passing this id as follows
facebook = new Facebook(APP_ID);
Using this instance, I am able to login to my FB account post on messages on facebook wall programatically.
After logging in, I get an access_token.
I'm using the access token to get the album ids using facebook.request("https://graph.facebook.com/me/albums?access_token="+facebook.getAccessToken());
Now I get {"error":{"message":"Malformed access token ACCESSTOKENACCESSTOKEN?access_token=ACCESSTOKENACCESSTOKEN","t‌​ype":"OAuthException","code":190}}
Can any of you please help me resolve this issue and point out what i am doing wrong.
My code is as follows:
private static final String[] PERMISSIONS = new String[] { "publish_stream","user_photos" };
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext()
.getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebook = new Facebook(APP_ID);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.facebook_dialog);
String facebookMessage = getIntent().getStringExtra("facebookMessage");
if (facebookMessage == null) {
facebookMessage = "Test wall post";
}
messageToPost = facebookMessage;
}
R.layout.facebook_dialog is the dialog which pops up asking if a message should be shared on facebook or not. If yes the following method is called.
public void share(View button) {
if (!facebook.isSessionValid()) {
loginAndPostToWall();
} else {
postToWall(messageToPost);
}
}
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null) {
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
finish();
}
}
public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("")
|| response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
getImagesFromUserAlbum();
finish();
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}
Later when I do a `private void getImagesFromUserAlbum() {
facebook.getAccessToken();
JSONArray albumss = null;
String response = null;
try {
response = facebook.request("me/albums");
// `
I get the error
{"error":{"message":"Malformed access token ACCESSTOKEN?access_token=ACCESSTOKEN","type":"OAuthException","code":190}}
Thanks for your help.
The code above is now the working copy. Thanks to Bartek.
If you look at the Errors page in the documentation you will see that when you get error 190 you should authorise/reauthorise the user.
I suspect that this happened to you because you first logged in, then added the permissions to access the albums to your application BUT did not log out and log back in. Hence, you need to obtain a new access token which will grant the new permissions to your application.
Please check is there &expires in your access token if yes then remove it because it is not part of access_token and try after that.

Categories