Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

ronS1's avatar
ronS1
Explorer | Level 4
8 years ago
Solved

Dropbox API not returning all files

 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

7 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    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's avatar
    ronS1
    Explorer | Level 4
    8 years ago
    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's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    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's avatar
    ronS1
    Explorer | Level 4
    8 years ago
    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's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    I believe I just commented on your StackOverflow post:

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

    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's avatar
    ronS1
    Explorer | Level 4
    8 years ago
    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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!