从Google云端硬盘上传到Dropbox

作为测试案例,我试图使用Google脚本将文件从Google Drive复制到Dropbox

function pushBuild() {
  // Setup OAuthServiceConfig
  var oAuthConfig = UrlFetchApp.addOAuthService("dropbox");
  oAuthConfig.setAccessTokenUrl("https://api.dropbox.com/1/oauth/access_token");
  oAuthConfig.setRequestTokenUrl("https://api.dropbox.com/1/oauth/request_token");
  oAuthConfig.setAuthorizationUrl("https://www.dropbox.com/1/oauth/authorize");
  oAuthConfig.setConsumerKey(ScriptProperties.getProperty("dropboxKey"));
  oAuthConfig.setConsumerSecret(ScriptProperties.getProperty("dropboxSecret"));

  var fileName = "blah.zip"
  var folderName = "upload_dir"

  var docs = DocsList.getFolder(folderName).find(fileName);
  for(n=0;n<docs.length;++n){
    if(docs[n].getName() == fileName){
      var ID = docs[n].getId();
      var options = {
        "oAuthServiceName" : "dropbox",
        "oAuthUseToken" : "always",
        "method" : "put",
        "payload" : docs[n].getBlob().getBytes(),
        "contentType" : "application/zip"        
    };

  var response = UrlFetchApp.fetch("https://api-content.dropbox.com/1/files_put/sandbox/upload_dir/" + fileName, options);

  Logger.log(response);
  }
 }

}

出现在Dropbox中的应用程序的授权请求,它告诉我我已经成功授权我的应用程序,但是当我检查时,该应用程序不在“我的应用程序”列表中,该文件尚未上传,并且没有日志中的条目。 目录“upload_dir”存在于GD和DB上。 我已经尝试了与“应用程序文件夹”和“完整Dropbox”应用程序类型相同的代码,但获得相同的结果。

另外,再次运行脚本会再次触发授权页面,类似于

这个

单击“允许”,然后显示成功屏幕,但该应用程序未列在“我的应用程序”中。 再次运行脚本会重复此过程。

任何人都可以指出我做错了什么?

更新

所以,我现在试图使用个人api调用来实现这一点,但我仍然没有取得任何成功。

function testOAuth() {

  var timestamp = getTimestamp();
  var nonce = getNonce(timestamp);

  var authString = 'OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_signature="' + encodeURIComponent(ScriptProperties.getProperty("dropboxSecret") + '&') + '", oauth_consumer_key="' + ScriptProperties.getProperty("dropboxKey") + '"';

  Logger.log(authString)

  var options = {
    method : "POST",
    headers : {"Authorization" : authString}
  }

  var response = UrlFetchApp.fetch("https://api.dropbox.com/1/oauth/request_token",options);  
  var params = response.getContentText().split("&");  
  var map = new Map;  
      for(i = 0; i < params.length; i++){
        var param = params[i].split("=");
        map.put(param[0],param[1]);        
      }

  var authStringx = "https://www.dropbox.com/1/oauth/authorize?oauth_token=" + map.get("oauth_token");

  Logger.log(authStringx);

  var response2 = UrlFetchApp.fetch(authStringx);

  Logger.log(response2.getContentText());

  var authString2 = 'OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_token="' + map.get("oauth_token") + '" , oauth_signature="' + encodeURIComponent(ScriptProperties.getProperty("dropboxSecret") + '&' + map.get("oauth_token_secret")) + '", oauth_consumer_key="' + ScriptProperties.getProperty("dropboxKey") + '",oauth_timestamp="'+ timestamp +'", oauth_nonce="' + nonce +'"';

  Logger.log(authString2);

  var options3 = {
    "method" : "POST",
    "Authorization" : authString2  
  }

  var response3 = UrlFetchApp.fetch("https://api.dropbox.com/1/oauth/access_token", options3);

  Logger.log(response3.getContentText());
}

var getTimestamp = function(){
  return (Math.floor((new Date()).getTime() / 1000)).toString()
}

var getNonce = function(timestamp){
  return timestamp + Math.floor( Math.random() * 100000000)
}

地图的代码实现在这里。 我可以看到的主要问题是,授权步骤不会调用Dropbox授权终结点(即,不会发生浏览器重定向来授权应用程序)。 如果我在Logger.log(authStringx);行后面放置一个断点Logger.log(authStringx); 并手动访问粘贴在authStringx的内容中的网页我得到屏幕授权我的应用程序。 我接受并收到该应用程序已在“我的应用程序”中注册的消息。 我现在让这个计划继续下去,我接到了这个消息

有任何想法吗?


PRAM,

我试图完成相同的任务,并遇到您的帖子。 我不是程序员,因此我无法弄清楚第二部分(启动授权页失败),但是我能够在第三步中完成该过程并成功连接我的应用程序。

代替:

var authString2 = 'OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_token="' + map.get("oauth_token") + '" , oauth_signature="' + encodeURIComponent(ScriptProperties.getProperty("dropboxSecret") + '&' + map.get("oauth_token_secret")) + '", oauth_consumer_key="' + ScriptProperties.getProperty("dropboxKey") + '",oauth_timestamp="'+ timestamp +'", oauth_nonce="' + nonce +'"';

Logger.log(authString2);

var options3 = {
"method" : "POST",
"Authorization" : authString2  
}

var response3 = UrlFetchApp.fetch("https://api.dropbox.com/1/oauth/access_token", options3);

Logger.log(response3.getContentText());

我用了:

var authtokenURL = "https://api.dropbox.com/1/oauth/access_token";

var authString2 = "?oauth_signature_method=PLAINTEXT&oauth_token=" + [MY_OAUTH_REQUEST_TOKEN] + "&oauth_signature=" + encodeURIComponent([MY_DROPBOX_CONSUMER_SECRET] + "&" + [MY_OAUTH_REQUEST_SECRET]) +"&oauth_consumer_key=" + [MY_DROPBOX_CONSUMER_KEY];

Logger.log(authString2);

var response3 = UrlFetchApp.fetch("https://api.dropbox.com/1/oauth/access_token" + authString2);    

Logger.log(response3.getContentText());

然后我收到一封电子邮件确认信息,表示我已将一个新应用程序连接到Dropbox,并且我的应用程序确实显示在我帐户的设置下。 无论如何,正如我所说的,我不是程序员,所以对于难看的代码感到抱歉。 感谢您为我提供代码,使其成为现实。 我希望这可以帮助你至少继续前进,即使它不能解决潜在的问题。


我也能看到这个问题。 Dropbox有一些特殊的功能。 你应该检查他们的论坛或他们的API支持团队。 看起来他们没有正确接受回调参数。 也许这是一种发展模式限制(相对于生产模式)。 或者,他们对Google不支持的某些POST和GET差异很严格。

下面的代码展示了您描述授权永远不会完成的相同问题。

function dropbox() {
  var oAuthCfg = UrlFetchApp.addOAuthService("dropbox");
  oAuthCfg.setAccessTokenUrl('https://api.dropbox.com/1/oauth/access_token');
  oAuthCfg.setRequestTokenUrl('https://api.dropbox.com/1/oauth/request_token');
  oAuthCfg.setAuthorizationUrl('https://api.dropbox.com/1/oauth/authorize');
  oAuthCfg.setConsumerKey('DROPBOX_KEY');
  oAuthCfg.setConsumerSecret('DROPBOX_SECRET');

  var options = {oAuthServiceName:'dropbox',oAuthUseToken:'always'}

  var url = 'https://api.dropbox.com/1/account/info';
  var response = UrlFetchApp.fetch(url, options).getContentText();
  Logger.log(response);
}

但是,相同的代码在Twitter OAuth 1 API中没有问题。 下面的代码应该从您的流中转储出JSON(一旦您从http://dev.twitter.com中的设置中替换了令牌

function twitter(){
  var oAuthCfg = UrlFetchApp.addOAuthService('twitter');
  oAuthCfg.setAccessTokenUrl('http://api.twitter.com/oauth/access_token');
  oAuthCfg.setRequestTokenUrl('http://api.twitter.com/oauth/request_token');
  oAuthCfg.setAuthorizationUrl('http://api.twitter.com/oauth/authorize');
  oAuthCfg.setConsumerKey('TWITTER_KEY');
  oAuthCfg.setConsumerSecret('TWITTER_SECRET');

  var options = {oAuthServiceName:'twitter',oAuthUseToken:'always'}

  var url = "http://api.twitter.com/1/statuses/user_timeline.json";
  var response = UrlFetchApp.fetch(url, options).getContentText();
  Logger.log(response);
}

如果您能够将此问题缩小到Google问题,请在问题跟踪器上记录一个错误。

链接地址: http://www.djcxy.com/p/11847.html

上一篇: Uploading to Dropbox from Google Drive

下一篇: Solving design involving multiple inheritance and composite classes in c++