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: 

Dropbox API not returning all files

Dropbox API not returning all files

ronS1
Explorer | Level 4
Go to solution
 public function __construct(Dropbox $dropbox)
    {
        $this->api_client = $dropbox->api();
        $this->content_client = $dropbox->content();
        $this->access_token = session('access_token');
    }

      public function dashboard()
    {
        return view('admin.dashboard');
    }

    public function user(){


        //dd($this->access_token);

        $response = $this->api_client->request('POST', '/2/users/get_current_account', [
            'headers' => [
                'Authorization' => 'Bearer ' . '$this->access_token'
            ]
        ]);

        $user = json_decode($response->getBody(), true);


        $page_data = [
            'user' => $user
        ];

        return view('admin.user', $page_data);
    }

access_token returns null and yells error: Invalid authorization value in HTTP header "Authorization": "Bearer" 

But when I hard code the access_token generated from the developers portal it works fine.

Is there a way to get the access token dynamically ?

Thanks in Advance

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution
For the error you're getting, it looks like you aren't passing in an access token. (I.e., "Bearer" should be followed by the access token.) You'll need to see why that is. It appears you're trying to retrieve it via `session('access_token')`, which apparently isn't working.

Anyway, yes, you can programmatically get an access token for the user using the OAuth 2 app authorization flow:

https://www.dropbox.com/developers/reference/oauth-guide

The documentation for that can be found here:

https://www.dropbox.com/developers/documentation/http/documentation#authorization

By the way, the subject for your post seems to be about another issue. If you're having another problem, please feel free to open another thread with the details.

View solution in original post

7 Replies 7

Greg-DB
Dropbox Staff
Go to solution
For the error you're getting, it looks like you aren't passing in an access token. (I.e., "Bearer" should be followed by the access token.) You'll need to see why that is. It appears you're trying to retrieve it via `session('access_token')`, which apparently isn't working.

Anyway, yes, you can programmatically get an access token for the user using the OAuth 2 app authorization flow:

https://www.dropbox.com/developers/reference/oauth-guide

The documentation for that can be found here:

https://www.dropbox.com/developers/documentation/http/documentation#authorization

By the way, the subject for your post seems to be about another issue. If you're having another problem, please feel free to open another thread with the details.

ronS1
Explorer | Level 4
Go to solution
public function login(Request $request)
    {
       if ($request->has('code')) {
            $data = [
                'code' => $request->input('code'),
                'grant_type' => 'authorization_code',
                'client_id' => env('DROPBOX_APP_KEY'),
                'client_secret' => env('DROPBOX_APP_SECRET'),
                'redirect_uri' => env('DROPBOX_REDIRECT_URI')
            ];
            $response = $this->api_client->request(
                'POST',
                '/oauth2/token',
                ['form_params' => $data]
            );

            $response_body = json_decode($response->getBody()->getContents(), true);
            
            $access_token = $response_body['access_token'];

            //dd($access_token); //I can access the token here

            return redirect('dashboard');
        }

        return redirect('/');
    }  
public function user(){

        $response = $this->adminRepo->apiClient->request('POST', '/2/users/get_current_account', [
            'headers' => [
                'Authorization' => 'Bearer ' . $this->adminRepo->accessToken
            ]
//error : invalid authorization value in HTTP header "Authorization": " (truncated...) ]); $user = json_decode($response->getBody(), true); $page_data = [ 'user' => $user ]; return view('admin.user', $page_data); }

Hey Greg, sorry about the post subject my bad.

 

Could you please take a look at above two methods in which I am trying to pass the access token generated by /oauth2/token but it returns null and throws an error of bad authorization header.Is there any possible way I can retrieve this token from within my other classes.

 

Thank you so much for your help. It means a lot me.

Greg-DB
Dropbox Staff
Go to solution
It sounds like you are able to retrieve the access token from the API, per your "I can access the token here" comment in your code, and that you're now trying to pass that string between your classes.

Unfortunately as that is more about structuring your app and passing data (in this case a string that has the access token) around your app, I'm afraid I can't offer help, as that's not about the Dropbox API itself.

A more general PHP forum may be of more help for guidance on how to pass around data like this between your classes.

ronS1
Explorer | Level 4
Go to solution
Yeah, Greg, I figured out the problem, I thank you for your help. There is one particular problem I would like to ask for your advice. As I can list all the files present in my dropbox folders how can I upload it directly to a website without downloading the file on my local machine?

I can generate the downloadable link to store it on my local machine, but as per my requirement, I need to grab a hold of the original file(binary) from Dropbox and import it.
Could you please shed some light on this is, it would be of a great help

Much appreciated. Thanks in advance

Greg-DB
Dropbox Staff
Go to solution
I believe I just commented on your StackOverflow post:

https://stackoverflow.com/questions/50127717/importing-files-from-dropbox-using-api-v2-to-a-serverwe...

I don't have full context on your app, but there are several ways to get the file data. If the Chooser works for your use case though, that's recommended as it's generally easy to use. You can get the link from the Chooser and use a standard HTPS client to GET the content from the link.

ronS1
Explorer | Level 4
Go to solution
The context of my app is to upload a whole Dropbox folder to my website and process the files inside it one by one. I can select multiple files from "chooser", but I'm not able to get hold of the actual folder in which the files reside. Can you please educate me on how a user can select a whole folder from his/her's Dropbox and upload it to my website and then I can process the files inside it.

Greg-DB
Dropbox Staff
Go to solution
If you want an entire folder, you can use the new "folderselect:true" option:

https://www.dropbox.com/developers/chooser

That can only be used with "preview" links, but you can modify those links for raw file access as documented here:

https://www.dropbox.com/help/desktop-web/force-download

For folders, that will give you a zip file. On your server, you can download the zip data, unzip it, and use the resulting files as desired.
Need more support?
Who's talking

Top contributors to this post

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