1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-11 22:24:13 +01:00
Arduino/hardware/arduino/avr/libraries/Bridge/examples/Temboo/UploadToDropbox/UploadToDropbox.ino

203 lines
7.3 KiB
Arduino
Raw Normal View History

2013-06-28 11:08:56 +02:00
/*
UploadToDropbox
Demonstrates uploading a file to Dropbox using the Temboo Arduino Yun SDK.
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
A Temboo account and application key are necessary to run all Temboo examples.
If you don't already have one, you can register for a free Temboo account at
http://www.temboo.com
You'll also need a valid Dropbox account, and OAuth credentials for Dropbox. To
obtain OAuth credentials for Dropbox, you'll need to register a Dropbox app at
2013-07-03 09:02:59 +02:00
https://www.dropbox.com/developers/apps -- the app name and domain can be whatever
you'd like. After registering the app, follow the instructions at
2013-06-28 11:08:56 +02:00
https://www.temboo.com/library/Library/Dropbox/OAuth/ to run the Initialize and Finalize
OAuth Choreos to complete the OAuth handshake and retrieve your Access Token information.
This example assumes basic familiarity with Arduino sketches, and that your Yun is connected
to the Internet.
Looking for another API? We've got over 100 in our Library!
This example code is in the public domain.
*/
#include <Bridge.h>
2013-07-08 15:09:09 +02:00
#include <Temboo.h>
2013-06-28 11:08:56 +02:00
#include "TembooAccount.h" // contains Temboo account information
// as described in the footer comment below
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
2013-07-03 09:02:59 +02:00
// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.
2013-06-28 11:08:56 +02:00
// your Dropbox app key, available on the Dropbox developer console after registering an app
const String DROPBOX_APP_KEY = "xxxxxxxxxx";
// your Dropbox app secret, available on the Dropbox developer console after registering an app
const String DROPBOX_APP_SECRET = "xxxxxxxxxx";
// your Dropbox access token, which is returned by the FinalizeOAuth Choreo
const String DROPBOX_ACCESS_TOKEN = "xxxxxxxxxx";
// your Dropbox access token secret, which is returned by the FinalizeOAuth Choreo
const String DROPBOX_ACCESS_TOKEN_SECRET = "xxxxxxxxxx";
boolean success = false; // a flag to indicate whether we've uploaded the file yet
void setup() {
Serial.begin(9600);
// For debugging, wait until a serial console is connected.
delay(4000);
while(!Serial);
Bridge.begin();
}
void loop()
{
// only try to upload the file if we haven't already done so
if (!success) {
Serial.println("Base64 encoding data to upload...");
// base64 encode the data to upload
String base64EncodedData = base64Encode("Hello, Arduino!");
Serial.println("Uploading data to Dropbox...");
// we need a Process object to send a Choreo request to Temboo
2013-07-08 15:09:09 +02:00
TembooChoreo UploadFileChoreo;
2013-06-28 11:08:56 +02:00
// invoke the Temboo client
2013-07-08 15:09:09 +02:00
// NOTE that the client must be reinvoked and repopulated with
// appropriate arguments each time its run() method is called.
UploadFileChoreo.begin();
2013-06-28 11:08:56 +02:00
// set Temboo account credentials
2013-07-08 15:09:09 +02:00
UploadFileChoreo.setAccountName(TEMBOO_ACCOUNT);
UploadFileChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
UploadFileChoreo.setAppKey(TEMBOO_APP_KEY);
2013-06-28 11:08:56 +02:00
// identify the Temboo Library choreo to run (Dropbox > FilesAndMetadata > UploadFile)
2013-07-08 15:09:09 +02:00
UploadFileChoreo.setChoreo("/Library/Dropbox/FilesAndMetadata/UploadFile");
2013-06-28 11:08:56 +02:00
// set the required choreo inputs
// see https://www.temboo.com/library/Library/Dropbox/FilesAndMetadata/UploadFile/
// for complete details about the inputs for this Choreo
// first specify the name of the file to create/update on Dropbox
2013-07-08 15:09:09 +02:00
UploadFileChoreo.addInput("FileName", "ArduinoTest.txt");
2013-06-28 11:08:56 +02:00
// next, the root folder on Dropbox relative to which the file path is specified.
// unless you're using an in-production Dropbox app, this should be left as "sandbox"
2013-07-08 15:09:09 +02:00
UploadFileChoreo.addInput("Root","sandbox");
2013-06-28 11:08:56 +02:00
// next, the Base64 encoded file data to upload
2013-07-08 15:09:09 +02:00
UploadFileChoreo.addInput("FileContents", base64EncodedData);
2013-06-28 11:08:56 +02:00
// finally, the Dropbox OAuth credentials defined above
2013-07-08 15:09:09 +02:00
UploadFileChoreo.addInput("AppSecret", DROPBOX_APP_SECRET);
UploadFileChoreo.addInput("AccessToken", DROPBOX_ACCESS_TOKEN);
UploadFileChoreo.addInput("AccessTokenSecret", DROPBOX_ACCESS_TOKEN_SECRET);
UploadFileChoreo.addInput("AppKey", DROPBOX_APP_KEY);
2013-06-28 11:08:56 +02:00
// tell the Process to run and wait for the results. The
2013-07-03 09:02:59 +02:00
// return code (returnCode) will tell us whether the Temboo client
2013-06-28 11:08:56 +02:00
// was able to send our request to the Temboo servers
2013-07-03 09:02:59 +02:00
unsigned int returnCode = UploadFileChoreo.run();
2013-06-28 11:08:56 +02:00
// a return code of zero (0) means everything worked
2013-07-03 09:02:59 +02:00
if (returnCode == 0) {
2013-06-28 11:08:56 +02:00
Serial.println("Success! File uploaded!");
success = true;
} else {
// a non-zero return code means there was an error
Serial.println("Uh-oh! Something went wrong!");
}
// print out the full response to the serial monitor in all
// cases, just for debugging
while (UploadFileChoreo.available()) {
2013-07-03 09:02:59 +02:00
char c = UploadFileChoreo.read();
Serial.print(c);
2013-06-28 11:08:56 +02:00
}
UploadFileChoreo.close();
2013-07-03 09:02:59 +02:00
Serial.println("Waiting...");
2013-06-28 11:08:56 +02:00
}
2013-07-03 09:02:59 +02:00
delay(30000); // wait 30 seconds between upload attempts
2013-06-28 11:08:56 +02:00
}
/*
A utility function to Base64 encode the specified string
by calling a Temboo Utilities Choreo.
*/
String base64Encode(String toEncode) {
// we need a Process object to send a Choreo request to Temboo
2013-07-08 15:09:09 +02:00
TembooChoreo Base64EncodeChoreo;
2013-06-28 11:08:56 +02:00
// invoke the Temboo client
2013-07-08 15:09:09 +02:00
Base64EncodeChoreo.begin();
2013-06-28 11:08:56 +02:00
// set Temboo account credentials
2013-07-08 15:09:09 +02:00
Base64EncodeChoreo.setAccountName(TEMBOO_ACCOUNT);
Base64EncodeChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
Base64EncodeChoreo.setAppKey(TEMBOO_APP_KEY);
2013-06-28 11:08:56 +02:00
// identify the Temboo Library choreo to run (Utilities > Encoding > Base64Encode)
2013-07-08 15:09:09 +02:00
Base64EncodeChoreo.setChoreo("/Library/Utilities/Encoding/Base64Encode");
2013-06-28 11:08:56 +02:00
// set choreo inputs
2013-07-08 15:09:09 +02:00
Base64EncodeChoreo.addInput("Text", toEncode);
2013-06-28 11:08:56 +02:00
// run the choreo
Base64EncodeChoreo.run();
// read in the choreo results, and return the "Base64EncodedText" output value.
// see http://www.temboo.com/arduino for more details on using choreo outputs.
while(Base64EncodeChoreo.available()) {
// read the name of the output item
String name = Base64EncodeChoreo.readStringUntil('\x1F');
name.trim();
// read the value of the output item
String data = Base64EncodeChoreo.readStringUntil('\x1E');
data.trim();
if(name == "Base64EncodedText") {
return data;
}
}
}
/*
2013-07-03 09:02:59 +02:00
IMPORTANT NOTE: TembooAccount.h:
2013-06-28 11:08:56 +02:00
2013-07-03 09:02:59 +02:00
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
by inserting your own Temboo account name and app key information. The contents of the file should
look like:
2013-06-28 11:08:56 +02:00
2013-07-03 09:02:59 +02:00
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
2013-06-28 11:08:56 +02:00
You can find your Temboo App Key information on the Temboo website,
under My Account > Application Keys
2013-07-03 09:02:59 +02:00
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
2013-06-28 11:08:56 +02:00
Keeping your account information in a separate file means you can save it once,
then just distribute the main .ino file without worrying that you forgot to delete your credentials.
*/