PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label dropbox. Show all posts
Showing posts with label dropbox. Show all posts

Wednesday, August 31, 2022

[FIXED] How can I use the newly revamped Dropbox-PHP API?

 August 31, 2022     api, dropbox, dropbox-php, pear, php     No comments   

Issue

I would like to use the Dropbox-PHP API that has recently come under development again. It is located here: http://code.google.com/p/dropbox-php/

I did cloned it with hg clone https://dropbox-php.googlecode.com/hg/ dropbox-php and I get this file structure:

Dropbox/API.php
Dropbox/autoload.php

Dropbox/Exception/Forbidden.php
Dropbox/Exception/NotFound.php
Dropbox/Exception/OverQuota.php
Dropbox/Exception/RequestToken.php
Dropbox/Exception.php

Dropbox/OAuth/PEAR.php
Dropbox/OAuth/PHP.php
Dropbox/OAuth/Zend.php
Dropbox/OAuth.php

examples/accountinfo.php
examples/createaccount.php
examples/download_image.php
examples/getmetadata.php
examples/oauth_workflow.php
examples/uploading.php

But I get this error when trying to run accountinfo.php (or example):

Warning: include(Dropbox/autoload.php) [function.include]: failed to open stream
No such file or directory in dropbox-api/examples/accountinfo.php on line 7

Right, so then I move the Dropbox folder inside of where all the example files are and still get an error message:

Fatal error: Uncaught exception 'Dropbox_Exception' with message 'The OAuth class
could not be found! Did you install and enable the oauth extension?' in
examples/Dropbox/OAuth/PHP.php:36 Stack trace: #0 examples/accountinfo.php(9):
Dropbox_OAuth_PHP->__construct('', '') #1 {main} thrown in
examples/Dropbox/OAuth/PHP.php on line 36

So I'm obviously not doing something right but I have no idea what.

Also saw on the site where it has instructions on installing:

pear channel-discover pear.dropbox-php.com
pear install dropbox-php/Dropbox-alpha

I ran those two commands and it still won't work. I don't usually have any problems coding in PHP but the lack of documentation is a little frustrating.

Update

As noted in the accepted answer below my main problem was not having oAuth installed on the system. I'm running OS X 10.6 - if someone can provide some clear and easy instructions on how to build / install this to work with XAMPP / PHP 5.3 I will accept your answer. I've tried the articles online about using homebrew and such but these are flaky and do not seem to work for me. Guessing I will have to build / install it from scratch.


Solution

The Dropbox folder needs to be inside one of the folders in your include_path.

Edit:
Also oauth needs to be "installed" on the system and included in php.ini (when you do phpinfo() oAuth should show up as a module). then things should work.



Answered By - mcrumley
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, July 31, 2022

[FIXED] How Can I use Dropbox Refresh Tokens in Google Apps Script to transfer Drive Files to Dropbox?

 July 31, 2022     dropbox, dropbox-api, file-upload, google-apps-script, google-drive-api     No comments   

Issue

Based on several examples, I wrote a function in Google apps script that takes a given input file and saves it at the specified location on Dropbox. It does this using a short lived access token obtained from the Dropbox apps console...

function driveToDropbox(inputFile, targetFilePath, writeMode='add') {
  //inputFile = Drive file object, targetFilePath = string, writeMode = 'overwrite' or 'add'
  inputFile = DriveApp.getFilesByName('temp.html').next(); //for testing
  targetFilePath = '/temp.html' //for testing

  var apiUrl = 'https://content.dropboxapi.com/2/files/upload';
  var dropboxAccessToken = '<SL Token>';

  var parameters = {
    path: targetFilePath,
    mode: writeMode,
    autorename: true,
    mute: false,
    strict_conflict: false,
  };

  var headers = {
    'Content-Type': 'application/octet-stream',
    Authorization: 'Bearer ' + dropboxAccessToken,
    'Dropbox-API-Arg': JSON.stringify(parameters),
  };

  var options = {
    method: 'POST',
    headers: headers,
    payload: inputFile.getBlob().getBytes(),
  };

  var response = JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());
  //Logger.log(response);

  Logger.log('File uploaded successfully');
}

This will only work for 4 hours, so I obtained the refresh token for the app through the standard flow, and with this cURL command via terminal I am able to generate new working SL tokens.

curl https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token=<R Token> -u <App key>:<App secret>

My problem is converting that CURL command into Google apps Javascript and pulling the returned SL token. This was my attempt...

var apiUrl = 'https://api.dropbox.com/oauth2/token';
var refreshToken = '<R Token>';
var appKey = '<App key>';
var appSecret = '<App secret>';
var appKeySecret = '<Combined app key+secret>'

var parameters = {
  refresh_token: refreshToken,
  client_id: appKey,
  client_secret: appSecret,
};

var headers = {
  'Content-Type': 'application/json',
  Authorization: 'Basic ' + appKeySecret
  'Dropbox-API-Arg': JSON.stringify(parameters),
};

var options = {
  method: 'POST',
  headers: headers,
  grant_type: "refresh_token",
  muteHttpExceptions: true,
};

var response = JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());
Logger.log(response);

My error response is "{error_description=missing required field "grant_type", error=unsupported_grant_type}" confirming that my issue is misunderstanding the formatting, but I'm not sure how to fix it.

After fixing that, I would parse out the SL token from the response and then use that with the working file upload code above. Is there something obvious that I am missing with the formatting?

EDIT: Here is the working function based on the selected answer. This should probably be broken into two functions.

function driveToDropbox(inputFile, targetFilePath, writeMode='add') {
  //inputFile = Drive file object, targetFilePath = string, writeMode = 'overwrite' or 'add'
  inputFile = DriveApp.getFilesByName('temp.html').next(); //for testing
  targetFilePath = '/temp.html'; //for testing

  ////Obtain new 4 hour SL access token with Refresh token
  var refreshToken = '<R Token>';
  var appKey = '<App key>';
  var appSecret = '<App secret>';

  var apiUrl = 'https://api.dropbox.com/oauth2/token';
  var options = {
    method: 'POST',
    headers: { "Authorization": "Basic " + Utilities.base64Encode(`${appKey}:${appSecret}`) },
    payload: {
      grant_type: "refresh_token",
      refresh_token: refreshToken
    },
    muteHttpExceptions: true,
  };
  var response = UrlFetchApp.fetch(apiUrl, options);
  var accessToken = JSON.parse(response.getContentText()).access_token;

  ////Transfer file with refreshed SL access token
  apiUrl = 'https://content.dropboxapi.com/2/files/upload';
  var parameters = {
    path: targetFilePath,
    mode: writeMode,
    autorename: true,
    mute: false,
    strict_conflict: false,
  };

  options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/octet-stream',
      Authorization: 'Bearer ' + accessToken,
      'Dropbox-API-Arg': JSON.stringify(parameters),
    },
    payload: inputFile.getBlob().getBytes(),
  };

  response = JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());

  Logger.log('File uploaded successfully');
}

Solution

I believe your goal is as follows.

  • You want to convert the following curl command to Google Apps Script.

    curl https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token= -u :

In this case, how about the following modification?

Modified script:

Please set your values of refreshToken, appKey, appSecret.

var refreshToken = '<R Token>';
var appKey = '<App key>';
var appSecret = '<App secret>';

var apiUrl = 'https://api.dropbox.com/oauth2/token';
var options = {
  method: 'POST',
  headers: { "Authorization": "Basic " + Utilities.base64Encode(`${appKey}:${appSecret}`) },
  payload: {
    grant_type: "refresh_token",
    refresh_token: refreshToken
  },
  muteHttpExceptions: true,
};
var response = UrlFetchApp.fetch(apiUrl, options);
var obj = JSON.parse(response.getContentText());
var accessToken = obj.access_token; // You can retrieve the access token.

console.log(accessToken);
  • When I saw your sample curl command, it seems that it is required to send grant_type and refresh_token as the form data. And, the basic authorization is used.

Note:

  • In this answer, it supposes that your sample curl command works. Please be careful about this.

Reference:

  • fetch(url, params)


Answered By - Tanaike
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing