cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Where should I locate the 'startOAuth2Authentication'?

Where should I locate the 'startOAuth2Authentication'?

Dolphin_2018
Explorer | Level 4
Go to solution

Hi,

 

Question 1:

 

I'm totally new to Android Studio and I have to add a DropBox (SDK V2) files loading method to an existing project. As I understood I have to add the following line to my code:

 

Auth.startOAuth2Authentication(UserActivity.this, getString(R.string.app_key));

as shows here in line 36:

 

https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/java/com/dropbox/c...

 

But where should I add it in MY code?

 

I tried to add the line to the place in my code that looks most logic to me, to the file "MainActivity.Java", here:

 

@TargetApi(23)
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
Auth.startOAuth2Authentication(UserActivity.this, getString(R.string.app_key));
  .
  .
  .
  .
}

but I'm getting an error: "Cannot resolve symbol 'Auth'".

So I tried to add the following to the top of this file as shown in the "Android" example project:

 

import com.dropbox.core.android.Auth;
import com.dropbox.core.examples.android.internal.OpenWithActivity;
import com.dropbox.core.v2.users.FullAccount;

and I'm getting the error: "Cannot resolve symbol 'core'"

 

I tried "Clear Project", "Rebuild Project" and also I tried "Invalidate Caches/Restart..." but the error still there.

 

What should I do in order to remove this error? and anyway what are all this "import" lines? are these names of a .jar files? where should I find them? and put them? or are these modules inside the DropBox SDK?

 

Question 2:

 

Later I have to add this to my code:

  

SharedPreferences prefs = getSharedPreferences("??????", MODE_PRIVATE);
String accessToken = prefs.getString("??????", null);

if (accessToken == null) {
    accessToken = Auth.getOAuth2Token();
}

 But what should be the first parameter in:

 

getSharedPreferences(...)

and in:

 

getString(...)

methods? should it be the name of my DropBox account's App? what is it?

 

I also tried to call only:

 

accessToken = Auth.getOAuth2Token();

but I'm getting 

accessToken = null...

Why ? does this method need the APP_KEY and the SECRET_KEY ?

 

Please advice,

Thanks ...

 

3 Accepted Solutions

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

[Cross-linking for reference: https://stackoverflow.com/questions/49070230/where-should-i-locate-startoauth2authentication ]

 

1) The startOAuth2Authentication method is what kicks off the Dropbox app authorization flow, so you should put it wherever in your app you handle the request from the user to connect their Dropbox account, e.g., the handler for tapping on a "Link to Dropbox" button in your app. (For example, that's what the Android example app does.)

 

The import lines are what import the necessary classes from the Dropbox SDK.  (You don't need the "OpenWithActivity" one for your app though.) The import/"can't resolve" issues you're seeing seem to indicate that you don't have the SDK successfully installed. We recommend using either Gradle or Maven to install it, as documented in the readme. Make sure you have one of those set up and working correctly. If you have tried that, and it's not working, please share whatever Maven/Gradle error you're getting.

 

2) That getSharedPreferences call is to the Android getSharedPreferences method. The first parameter there is the "name" for the "Desired preferences file". You can supply whatever name you want.

 

That getString call is to the Android getString method. The first parameter is the "key" for "The name of the preference to retrieve". Likewise, the exact string is up to you. 

 

With both of these, it's mostly just important that you're consistent with the strings you use, as it's how you will store and retrieve the account information. There's an example of these being used to store and retrieve the Dropbox access token in the Android example.

 

The getOAuth2Token method retrieves the access token, but only when the user is actually going through the app authorization flow. If they are not, it will return null. You should store and re-use the access token when you get it. That's done in the example here.

View solution in original post

Greg-DB
Dropbox Staff
Go to solution
That line should run when you click on the "Login with Dropbox" button, not the "+" button. If you're seeing the "+", it means you've already connected Dropbox.

Anyway, I just tried it and it's working for me. (Also, make sure that you're actually debugging, and not just running. I.e., use the "bug" icon when in Android Studio, not the "play" icon.)

View solution in original post

Greg-DB
Dropbox Staff
Go to solution
Make sure you have AuthActivity registered in your app's package, and only in one, as shown here:

https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/AndroidManifest.xm...

If you had others registered but removed them, try restarting your device.

View solution in original post

11 Replies 11

Greg-DB
Dropbox Staff
Go to solution

[Cross-linking for reference: https://stackoverflow.com/questions/49070230/where-should-i-locate-startoauth2authentication ]

 

1) The startOAuth2Authentication method is what kicks off the Dropbox app authorization flow, so you should put it wherever in your app you handle the request from the user to connect their Dropbox account, e.g., the handler for tapping on a "Link to Dropbox" button in your app. (For example, that's what the Android example app does.)

 

The import lines are what import the necessary classes from the Dropbox SDK.  (You don't need the "OpenWithActivity" one for your app though.) The import/"can't resolve" issues you're seeing seem to indicate that you don't have the SDK successfully installed. We recommend using either Gradle or Maven to install it, as documented in the readme. Make sure you have one of those set up and working correctly. If you have tried that, and it's not working, please share whatever Maven/Gradle error you're getting.

 

2) That getSharedPreferences call is to the Android getSharedPreferences method. The first parameter there is the "name" for the "Desired preferences file". You can supply whatever name you want.

 

That getString call is to the Android getString method. The first parameter is the "key" for "The name of the preference to retrieve". Likewise, the exact string is up to you. 

 

With both of these, it's mostly just important that you're consistent with the strings you use, as it's how you will store and retrieve the account information. There's an example of these being used to store and retrieve the Dropbox access token in the Android example.

 

The getOAuth2Token method retrieves the access token, but only when the user is actually going through the app authorization flow. If they are not, it will return null. You should store and re-use the access token when you get it. That's done in the example here.

Dolphin_2018
Explorer | Level 4
Go to solution

Thanks very much, I'm trying to implement it right now. In the meantime, I'm just wondering, why when I'm putting a breakpoint on the line:

 

Auth.startOAuth2Authentication(UserActivity.this, getString(R.string.app_key));

 

in the "Android" example, it's never stopping there? does it makes sense? it looks like the code is working properly and when I run it on my Nexus 5 device, click the "+" sign on the right bottom of the screen, and choose a file from my pictures collection, it succesfully transmited to my dropbox folder, but as I said it's never stop in the breakpoint.

 

How can it be? am I missing something in my understanding of this code?

 

Thanks.

 

Greg-DB
Dropbox Staff
Go to solution
That line should run when you click on the "Login with Dropbox" button, not the "+" button. If you're seeing the "+", it means you've already connected Dropbox.

Anyway, I just tried it and it's working for me. (Also, make sure that you're actually debugging, and not just running. I.e., use the "bug" icon when in Android Studio, not the "play" icon.)

Dolphin_2018
Explorer | Level 4
Go to solution

Thanks very much now It's understood... I thought that it should Login to the Dropbox on every new run so I thought that it should stop on the breakpoint every time.

 

And yes I was in debugging mode (Shift + F9 on the Android Studio).

 

I will continue with this next week and let you know.

 

Thanks!

 

Dolphin_2018
Explorer | Level 4
Go to solution

Hi Greg,

 

1. As I understand I should call:

 

startOAuth2Authentication(...);

only once, does it matter where I call it? can I just call it in the OnCreate() method of my class? 

 

2. I tried to call it from several places and every time it's cause my application to crush.

 

The call is something like that:

 

Auth.startOAuth2Authentication(className.this, "7ekr2fh5e4d82b2");

 I tried to place it in the OnCreate(), and also in the method that is called when the user click on the "Link to DropBox" link, but it's crushing enywhere I put it.

 

What may be the problem?

 

Greg-DB
Dropbox Staff
Go to solution

1. You will generally only need to call `startOAuth2Authentication` once per user. Note that the user can revoke access though, so you should provide UI for allowing them to link again later if they want.

 

You can technically put it in OnCreate, but it usually makes sense to only call it when the user explicitly indicates they want to link to Dropbox.

 

2. Please the error and stack for the crash and I'll take a look.

Dolphin_2018
Explorer | Level 4
Go to solution

Hi Greg,

 

I still have to learn how to use the Stack Trace, so for now I used something like this:

 

try
{
  Auth.startOAuth2Authentication(mContext, "2eh.........8g0");
}
catch(Exception Ex)
{
    errorStr = Ex.getMessage();

     .....
}

 

And the errorStr shows:

 

java.lang.IllegalStateException: There must be a com.dropbox.core.android.AuthActivity within your app's package registered for your URI scheme (db-2eh.........8g0). However, it appears that an activity in a different package is registered for that scheme instead. If you have multiple apps that all want to use the same accesstoken pair, designate one of them to do authentication and have the other apps launch it and then retrieve the token pair from it.

 

Does it helps? (by the way just to be sure I uninstalled all the apps of this kind from my device, but I still have the same error). 

 

Greg-DB
Dropbox Staff
Go to solution
Make sure you have AuthActivity registered in your app's package, and only in one, as shown here:

https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/AndroidManifest.xm...

If you had others registered but removed them, try restarting your device.

Dolphin_2018
Explorer | Level 4
Go to solution

Hi,

 

Thanks very much it's working now.

 

I have another question about getting the account username, please see here:

 

https://stackoverflow.com/questions/49262951/what-is-the-equivalent-for-getaccountinfo-username-in-d...

 

Thanks.

 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Dolphin_2018 Explorer | Level 4
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?