I need to authenticate multiple accounts
I have searched the forum, and it seems like it is possible
So I gave it a try, but I failed
I had tried using the same API APP_KEY & APP_SECRET, it failed
Both my session return the same access tokens pair
So I try using different API APP_KEY & APP_SECRET, under same Dropbox account, it failed too
So I try again using different API APP_KEY & APP_SECRET from different Dropbox accounts, it still failed
Anyone can provide me a solution? Thanks in advance
Below is my code, mainly comes from the DBroulette example
onCreate (android)
AndroidAuthSession session = buildSession();
mApi = new DropboxAPI<AndroidAuthSession>(session);
AndroidAuthSession session2 = buildSession2();
mApi2 = new DropboxAPI<AndroidAuthSession>(session2);
onResume (android)
AndroidAuthSession session = mApi.getSession();
if (session.isLinked()) {
dbsetLoggedIn(true);
} else {
dbsetLoggedIn(false);
}
if (session.authenticationSuccessful()) {
try {
session.finishAuthentication();
TokenPair tokens = session.getAccessTokenPair();
dbstoreKeys(tokens.key, tokens.secret);
dbsetLoggedIn(true);
statusTv.append("Dropbox authentication successful\n");
} catch (IllegalStateException e) {
Log.i("Dropbox Error", "Error authenticating", e);
}
}
AndroidAuthSession session2 = mApi2.getSession();
if (session2.isLinked()) {
dbsetLoggedIn2(true);
} else {
dbsetLoggedIn2(false);
}
if (session2.authenticationSuccessful()) {
try {
session2.finishAuthentication();
TokenPair tokens = session2.getAccessTokenPair();
dbstoreKeys2(tokens.key, tokens.secret);
dbsetLoggedIn2(true);
statusTv.append("2Dropbox authentication successful\n");
} catch (IllegalStateException e) {
Log.i("Dropbox Error", "Error authenticating", e);
}
}
OTHERS CODES
private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(Constants.APP_KEY, Constants.APP_SECRET);
AndroidAuthSession session;
String[] stored = getKeys();
if (stored != null) {
AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE, accessToken);
} else {
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE);
}
return session;
}
private AndroidAuthSession buildSession2() {
AppKeyPair appKeyPair = new AppKeyPair(Constants.APP_KEY2, Constants.APP_SECRET2);
AndroidAuthSession session;
String[] stored = getKeys2();
if (stored != null) {
AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE, accessToken);
} else {
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE);
}
return session;
}
private String[] getKeys() {
SharedPreferences prefs = getSharedPreferences(Constants.ACCOUNT_PREFS_NAME, 0);
String key = prefs.getString(Constants.ACCESS_KEY_NAME, null);
String secret = prefs.getString(Constants.ACCESS_SECRET_NAME, null);
if (key != null && secret != null) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}
private String[] getKeys2() {
SharedPreferences prefs = getSharedPreferences(Constants.ACCOUNT_PREFS_NAME, 0);
String key = prefs.getString(Constants.ACCESS_KEY_NAME2, null);
String secret = prefs.getString(Constants.ACCESS_SECRET_NAME2, null);
if (key != null && secret != null) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}
I noticed that I MAYBE need to add something into the manifest in the adding another
BUT I cannot add second activity in android manifest with different APP KEY because it will cause duplicated error
How can I do it?
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:configChanges="orientation|keyboard"
android:launchMode="singleTask" >
<intent-filter>
<data android:scheme="db-XXXXXXXXXXXX" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I'm not sure if this would help you a little bit in your use case, but maybe it could be a workaround to write your own authenticator to use the Android build-in account management to seperate the authentication processes.
Here is an example: http://udinic.wordpress.com/2013/04/24/write-your-own-android-authenticator/
I ran into a similar requirement and this is how I worked around.
1st App
Get access for your first application using the normal dropbox flow.
Note:
A likely case for 2 dropbox applications requirement could be accessing user account from your server using a different dropbox application. Please note that you can share the access tokens from 1st app with your server and reuse these credentials safely, provided you are using the same dropbox application on server. If you can't live with that, continue reading.
2nd App
Option 1: Using another Android app
Create another Android App just for the oAuth flow for 2nd dropbox app.
Use Intent to trigger oAuthflow in app2 from app1.
Again, use intent to send back token data from app2 to app1
A few tips, if you are going to use this:
Make the background of App2 oAuth Activity transparent
Remove Intent change animations for app1 <-> app2 transitions
Trigger oAuth in App2 Activity's onCreate
Option 2: If you are keep on doing this with only one Android app, I found a possible workaround as described below.
Prompt your user to open this url:
https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=APP2_CLIENT_ID
They will have to copy back an authorization code returned by Dropbox
This authorization code can be used to obtain access_tokens for 2nd app
If you are going to use the 2nd app in a server side context, simply share the authorization code with your server. You can obtain tokens from authorization code, in a python flow, like this:
flow = client.DropboxOAuth2FlowNoRedirect(app2_key, app2_secret)
authorize_url = flow.start()
access_token, user_id = flow.finish(auth_code_from_client)
For more generic ways to obtain access_tokens from authorization keys, look at this
Dropbox API is having some issues or you can say a trick that you need to use in order to do multiple logins.
1. Declare sAuthenticatedUid as String[]
private static final String[] sAuthenticatedUid = { "dummy"}; // Keeping only one Auth Id to keep last authenticated item
2. Start OAuth using different method
Use session.startOAuth2Authentication(act, "", sAuthenticatedUid) for authentication instead of startOAuth2Authentication()
3. Maintain Variables on authentication Success
sAuthenticatedUid[0] = sessionApi.getSession().finishAuthentication(); // Save the last successful UID
String oauth2AccessToken = sessionApi.getSession().getOAuth2AccessToken();
AuthActivity.result = null; // Reset this so that we can login again, call only after finishAuthentication()
AuthActivity is com.dropbox.client2.android.AuthActivity which stores the result from last authentication and can create problems as this is static variable.
You should be able to do as many logins as you want now.
Related
i have multiple places where my app should upload files to Dropbox. when i try to upload a file the app is falling back to browser. if i use the DropboxApi object globally it says DropboxUnlinkedException.
I am posting my code
Dropbox.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We create a new AuthSession so that we can use the Dropbox API.
AndroidAuthSession session = buildSession();
AJ_Constant.mApi = new DropboxAPI<AndroidAuthSession>(session);
// Basic Android widgets
// setContentView(R.layout.main);
if (mLoggedIn) {
logOut();
} else {
// Start the remote authentication
AJ_Constant.mApi.getSession().startAuthentication(DropBox.this);
}
}
#Override
protected void onResume() {
super.onResume();
if (session.authenticationSuccessful()) {
try {
// Mandatory call to complete the auth
session.finishAuthentication();
// Store it locally in our app for later use
TokenPair tokens = session.getAccessTokenPair();
storeKeys(tokens.key, tokens.secret);
} catch (IllegalStateException e) {
showToast("Couldn't authenticate with Dropbox:"
+ e.getLocalizedMessage());
Log.i(TAG, "Error authenticating", e);
}
}
}
The code for uploading file is :
(this code is in another file _someX.java)
com.dropbox.client2.DropboxAPI.Entry response = AJ_Constant.mApi.putFile(
AJ_Constant.ReportfileName, inputStream, file.length(),
null, null);
Should i re-build the session or get authentication process done everytime??
Please suggest me any solution
Thanks in advance
From the Android SDK docs (note the last line of code):
A typical authentication flow when no user access token pair is saved
is as follows:
AndroidAuthSession session = new AndroidAuthSession(myAppKeys, myAccessType);
// When user wants to link to Dropbox, within an activity:
session.startOAuth2Authentication(this);
// When user returns to your activity, after authentication:
if (session.authenticationSuccessful()) {
try {
session.finishAuthentication();
AccessTokenPair tokens = session.getAccessTokenPair();
// Store tokens.key, tokens.secret somewhere
} catch (IllegalStateException e) {
// Error handling
}
}
When a user returns to your app and you have tokens stored, just
create a new session with them:
AndroidAuthSession session = new AndroidAuthSession(
myAppKeys, myAccessType, new AccessTokenPair(storedAccessKey, storedAccessSecret));
I am creating an box android app that allows user to upload media files on their account.
I have set up my client id and client secret,it is authenticating my app too.
Uploading part is also done,but the problem i am facing is to save the auth data [which is obviously needed so user is not needed to login again and again]
Load, save and use of authentication data in Box Android API
the solution given above is not working [may b they have removed 'Utils.parseJSONStringIntoObject' method]
i can store the access token and refresh token but whats the point of saving when i cant use them to re authenticate a user
switch (requestCode)
{
case AUTHENTICATE_REQUEST:
if (resultCode == Activity.RESULT_CANCELED)
{
String failMessage = data.getStringExtra(OAuthActivity.ERROR_MESSAGE);
Toast.makeText(this, "Auth fail:" + failMessage, Toast.LENGTH_LONG).show();
// finish();
}
else
{
BoxAndroidOAuthData oauth = data.getParcelableExtra(OAuthActivity.BOX_CLIENT_OAUTH);
BoxAndroidClient client = new BoxAndroidClient(BoxSDKSampleApplication.CLIENT_ID, BoxSDKSampleApplication.CLIENT_SECRET, null, null);
client.authenticate(oauth);
String ACCESS_TOKEN=oauth.getAccessToken();
String REFRESH_TOKEN=oauth.getRefreshToken();
Editor editor = prefs.edit();
editor.putString("ACCESS_TOKEN", ACCESS_TOKEN);
editor.putString("REFRESH_TOKEN", REFRESH_TOKEN);
editor.commit();
BoxSDKSampleApplication app = (BoxSDKSampleApplication) getApplication();
client.addOAuthRefreshListener(new OAuthRefreshListener()
{
#Override
public void onRefresh(IAuthData newAuthData)
{
Log.d("OAuth", "oauth refreshed, new oauth access token is:" + newAuthData.getAccessToken());
//---------------------------------
BoxOAuthToken oauthObj=null;
try
{
oauthObj=getClient().getAuthData();
}
catch (AuthFatalFailureException e)
{
e.printStackTrace();
}
//saving refreshed oauth object in client
BoxAndroidOAuthData newAuthDataObj=new BoxAndroidOAuthData(oauthObj);
getClient().authenticate(newAuthDataObj);
}
});
app.setClient(client);
}
i have referred https://github.com/box/box-android-sdk-v2/tree/master/BoxSDKSample example
can any one tell me what i am doing wrong or any alternative to authenticate user using authdata,access token,refresh token?
UPDATE
refreshing token as they have said
'Our sdk auto refreshes OAuth access token when it expires. You will want to listen to the refresh events and update your stored token after refreshing.'
mClient.addOAuthRefreshListener(new OAuthRefreshListener()
{
#Override
public void onRefresh(IAuthData newAuthData)
{
Log.d("OAuth", "oauth refreshed, new oauth access token is:" + newAuthData.getAccessToken());
try
{
oauthObj=mClient.getAuthData();
mClient.authenticate(newAuthData);
String authToken=null;
//Storing oauth object in json string format
try
{
authToken = new BoxJSONParser(new AndroidBoxResourceHub()).convertBoxObjectToJSONString(newAuthData);
prefs.edit().putString("BOX_TOKEN", authToken).commit();
//saving authToken in shared Preferences
mClient.authenticate(newAuthData);
String ACCESS_TOKEN=newAuthData.getAccessToken();
String REFRESH_TOKEN=newAuthData.getRefreshToken();
Log.v("New Access token ", oauthObj.getAccessToken());
Log.v("New Refresh token ", oauthObj.getRefreshToken());
editor.putString("ACCESS_TOKEN", ACCESS_TOKEN);
editor.putString("REFRESH_TOKEN", REFRESH_TOKEN);
prefs.edit().putString("BOX_TOKEN", authToken).commit();
editor.commit();
}
catch (BoxJSONException e1)
{
e1.printStackTrace();
}
Log.v("Token Refreshed", " ");
}
catch (AuthFatalFailureException e)
{
e.printStackTrace();
}
}
});
app.setClient(mClient);
}
onClientAuthenticated();
In main activity,fetching stored token
try
{
stored_oauth_token=prefs.getString("BOX_TOKEN", null);
authData = new BoxJSONParser(new AndroidBoxResourceHub()).parseIntoBoxObject(stored_oauth_token, BoxAndroidOAuthData.class);
}
catch (BoxJSONException e)
{
e.printStackTrace();
}
mClient = new BoxAndroidClient(BoxSDKSampleApplication.CLIENT_ID, BoxSDKSampleApplication.CLIENT_SECRET, null, null);
mClient.authenticate(authData);
BoxSDKSampleApplication app = (BoxSDKSampleApplication) getApplication();
app.setClient(mClient);
i tried this app to upload a file after existing ,it did work
but after 60-70 odd minutes i couldn't upload file.
is there anything wrong in my code ?
This is how I initialize my Box client:
mClient = new BoxClient(BOX_CLIENT_ID, BOX_CLIENT_SECRET, null, null);
mClient.addOAuthRefreshListener(new OAuthRefreshListener() {
#Override
public void onRefresh(IAuthData newAuthData) {
try {
String authToken = new BoxJSONParser(new AndroidBoxResourceHub()).convertBoxObjectToJSONString(newAuthData);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putString("box_token", authToken).commit();
} catch (BoxJSONException e) { }
}
});
mAuthToken = prefs.getString("box_token", null);
if (mAuthToken != null) {
BoxAndroidOAuthData authData = new BoxJSONParser(
new AndroidBoxResourceHub()
).parseIntoBoxObject(mAuthToken, BoxAndroidOAuthData.class);
mClient.authenticate(authData);
}
if (!mClient.isAuthenticated()) {
Intent intent = OAuthActivity.createOAuthActivityIntent(context, BOX_CLIENT_ID, BOX_CLIENT_SECRET, false, "https://yoururl.com/");
((Activity) context).startActivityForResult(intent, BOX_AUTH_REQUEST_CODE);
}
So for the auth refresh there are a couple of things to be considered:
box client automatically refreshes OAuth tokens, you'll want to attach a OAuthRefreshListener to listen to the refresh, if you want to persist, persist the oauth data passed into the refresh listener. The listener only update your persisted oauth data, you don't need to re-authenticate in the refresh listener, sdk does the re-authenticate automatically.
When you first initiate box client, you need to authenticate either by persisted auth, or the OAuth UI. The logic should be:
check client.isAuthenticated();
2.1 If authenticated, do nothing.
2.2 if not authenticated, try to check whether there's persisted auth data. If so, authenticate by client.authenticate(oauthdata);
2.3 if 2.2 failed, start OAuth UI flow.
2.4 at last, in case of OAuthFatalFailureException, start OAuth UI flow.
I want to use the dropbox in my application.I developed a sample application for upload and download files and it ask for authentication.
But I don't want to open login popup.
Is it possible access the dropbox by other users using default account(single account) login details?
So any user can use dropbox directly without login popup.
How to set access user access token pair manually.
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
if (mDBApi == null) {
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
// mDBApi.getSession().startAuthentication(Main.this); //kicks off the web-based authentication
//you'll have to use the web-based authentication UI one-time to get the ######### values
String token_key="#########";
String token_seceret="#########";
AccessTokenPair tokens=new AccessTokenPair(token_key,token_seceret);
mDBApi.getSession().setAccessTokenPair(tokens);
// boolean v=mDBApi.getSession().authenticationSuccessful();
}
First time i run application in debug mode with break point i get the token key and token secret of by entering valid log in detail.and saved(noted) that credential and after that i set them manually as above code then can be log in successfully.
Yes. Have a look at their example app DBRoulette.
Please download the project from the below link name as DBRoulette
https://www.dropbox.com/developers/core
And create an app in https://www.dropbox.com/developers and get the api key and secret and add this both in DBRoulette.java and in AndroidManifest.xml ...it works..
In onCreate() write
AppKeyPair pair = new AppKeyPair(ACCESS_KEY, ACCESS_SECRET);
session = new AndroidAuthSession(pair, AccessType.APP_FOLDER);
dropbox = new DropboxAPI<AndroidAuthSession>(session);
SharedPreferences prefs = getSharedPreferences(DROPBOX_NAME, 0);
String key = prefs.getString(ACCESS_KEY, null);
String secret = prefs.getString(ACCESS_SECRET, null);
if (key != null && secret != null) {
Log.d("key secret", key + " " + secret);
AccessTokenPair token = new AccessTokenPair(key, secret);
dropbox.getSession().setAccessTokenPair(token);
}
if (key == null && secret == null)
dropbox.getSession().startAuthentication(DropboxActivity.this);
And in onResume() write
if (dropbox.getSession().isLinked()) {
try {
loggedIn(true);
doAction();
} catch (IllegalStateException e) {
Toast.makeText(this, "Error during Dropbox authentication",
Toast.LENGTH_SHORT).show();
}
} else if (dropbox.getSession().authenticationSuccessful()) {
try {
session.finishAuthentication();
TokenPair tokens = session.getAccessTokenPair();
SharedPreferences prefs = getSharedPreferences(DROPBOX_NAME, 0);
Editor editor = prefs.edit();
editor.putString(ACCESS_KEY, tokens.key);
editor.putString(ACCESS_SECRET, tokens.secret);
editor.commit();
loggedIn(true);
doAction();
} catch (IllegalStateException e) {
Toast.makeText(this, "Error during Dropbox authentication",
Toast.LENGTH_SHORT).show();
}
}
It worked fine for me
I'm trying to create on android a facebook application and I'm using android facebook-sdk .
The example that I'm trying to understand is this one:
https://github.com/facebook/facebook-android-sdk/tree/master/examples/stream
There is something that I don't understand in here if u could help me out a little bit it would be great.
At some point in the main Activity is doing something like:
Dispatcher dispatcher = new Dispatcher(this);
dispatcher.addHandler("login", LoginHandler.class);
dispatcher.addHandler("stream", StreamHandler.class);
dispatcher.addHandler("logout", LogoutHandler.class);
Session session = Session.restore(this);
if (session != null) {
dispatcher.runHandler("stream");
} else {
dispatcher.runHandler("login");
}
}
What I don't understand is the way this Session.restore(this) works.
The restore method looks like this:
public static Session restore(Context context) {
if (singleton != null) {
if (singleton.getFb().isSessionValid()) {
return singleton;
} else {
return null;
}
}
SharedPreferences prefs =
context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
String appId = prefs.getString(APP_ID, null);
if (appId == null) {
return null;
}
Facebook fb = new Facebook(appId);
fb.setAccessToken(prefs.getString(TOKEN, null));
fb.setAccessExpires(prefs.getLong(EXPIRES, 0));
String uid = prefs.getString(UID, null);
String name = prefs.getString(NAME, null);
if (!fb.isSessionValid() || uid == null || name == null) {
return null;
}
Session session = new Session(fb, uid, name);
singleton = session;
return session;
}
If someone could explain me what is the whole purpose of SharedPreferences, what is stored there and why are these 2 lines needed :
fb.setAccessToken(prefs.getString(TOKEN, null));
fb.setAccessExpires(prefs.getLong(EXPIRES, 0));
When you access any facebook user information or any other action which requires permission to be accessed as shown below. . If the user press Allow button then A Token is inserted in their Database with the user Id , your App Id and the validation time (which may be unlimited) as well as the Actions you may perform (e.g Access Info, Send Email, Access Posts, Post to Wall etc.), that specific Token is returned to you and you save that Token to access the info and other action which are permitted against that token.
Whenever you make a request for any action they match that token, check validation and then see if that action is allowed by the user, if allowed you are granted to complete the action.
i developed one app integrated with linkedIn..!
i do SignIn authentication in linkedIn using OAuth Service to post the Network Update..but now how to sign out (de-authenticate) to the LinkedIn automatically?
Thanks in adv..
As per the official blog
Token Invalidation
Now you can invalidate an OAuth token for your application. Just send an OAuth signed GET request to:
https://api.linkedin.com/uas/oauth/invalidateToken
A 200 response indicates that the token was successfully invalidated.
However as per this :
Third party applications do not have any way to log a user out from
LinkedIn - this is controlled by the website. Invalidating the token
makes the user re-authorize the next time they try to use the
application, but once they have logged into LinkedIn their browser
will remain logged in until they log out via the website.
So In conclusion : as of this date of writing, Linked In does not give this support to 3rd Party Applications
Reading your question i have also tried to find solution and also talked to Mr. Nabeel Siddiqui - Author of linkedin-j API
and this was his reply when i asked if it's possible to sign out using linkedin-j api?
Hi Mayur
There is a method LinkedInOAuthService#invalidateAccessToken that is supposed to invalidate your access token. Its not used much by the community so I am not sure if it works as expected or not. Do try it and let me know if there are problems.
Regards
Nabeel Mukhtar
so in my activity i tried it using this way.
final LinkedInOAuthService oAuthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(consumerKey, consumerSecret);
final LinkedInApiClientFactory factory = LinkedInApiClientFactory.newInstance(consumerKey, consumerSecret);
LinkedInRequestToken liToken;
LinkedInApiClient client;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
liToken = oAuthService.getOAuthRequestToken(CALLBACKURL);
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(liToken.getAuthorizationUrl()));
startActivity(i);
}
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACKURL))
{
String verifier = intent.getData().getQueryParameter("oauth_verifier");
LinkedInAccessToken accessToken = oAuthService.getOAuthAccessToken(liToken, verifier);
client = factory.createLinkedInApiClient(accessToken);
Connections con = client.getConnectionsForCurrentUser();
//AFTER FETCHING THE DATA I HAVE DONE
oAuthService.invalidateAccessToken(accessToken);
//this is for sign out
}
}
Please, Try this way once and tell me if it solves your problem.
cause I have also donwloaded and seen the SourceCode for linkedin-j API and in
LinkedInOAuthServiceImpl.java
they have given the function and that function also works if we write the same code in our file.
that is,
#Override
public void invalidateAccessToken(LinkedInAccessToken accessToken) {
if (accessToken == null) {
throw new IllegalArgumentException("access token cannot be null.");
}
try {
URL url = new URL(LinkedInApiUrls.LINKED_IN_OAUTH_INVALIDATE_TOKEN_URL);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
final OAuthConsumer consumer = getOAuthConsumer();
consumer.setTokenWithSecret(accessToken.getToken(), accessToken.getTokenSecret());
consumer.sign(request);
request.connect();
if (request.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new LinkedInOAuthServiceException(convertStreamToString(request.getErrorStream()));
}
} catch (Exception e) {
throw new LinkedInOAuthServiceException(e);
}
}