使用CloudRail进行Dropbox身份验证

时间:2016-01-18 13:36:47

标签: api cloud integration dropbox

我尝试使用CloudRail集成Dropbox API,但我遇到了一些问题:

  1. 如何通过CloudRail API检索访问令牌
  2. 当我在简单的控制台应用程序中进行编码时,redirecURI是什么。
  3. 最后我需要System.out.print方法中的onSuccess
  4. 以下是我的尝试:

    
    package com.cloudrail.userInterface;

    import java.io.InputStream; import com.cloudrail.auth.UriRedirectListener; import com.cloudrail.auth.UriView; import com.cloudrail.auth.uriRedirect.HttpsServerRedirectListener; import com.cloudrail.auth.uriView.LocalUriCaller; import com.cloudrail.exception.CloudRailException; import com.cloudrail.userInterface.CloudDownload.DropboxDLResponseListener; public class CloudDownloadTest { final static String dropboxID = "someID"; // Put your Dropbox client id final static String dropboxSecret = "someSecret"; // Put your Dropbox client secret final static String dropboxCloudFilePath = "afile.txt"; // Put the path to the file that should be downloaded from Dropbox final static String dropboxDiskFilePath = "somePath/CopiedDPBFile"; // Put the path the Dropbox file should be downloaded to // I have questions about redirectURI - What is it how can I figure it out or take from somwehere. // Can I just give the hhtps://www.dropbox.com or it should be the site url where I integrate the API. final static String redirectURI = "soemURIString"; final static String state = Long.toHexString(Double.doubleToLongBits(Math.random())); // Random state to prevent CSRF // First argument is a dummy SSL context for local testing final static UriRedirectListener uriRedirectListener = new HttpsServerRedirectListener(Helper.getDummyContext(), 8080); final static UriView localUriCaller = new LocalUriCaller(); // Opens URLs in the local browser public static void main(String[] args) { // TODO Auto-generated method stub CloudDownload cloudDownload = new CloudDownload(dropboxID, redirectURI, dropboxSecret, state); cloudDownload.init_DropboxContentAPI_Auth(localUriCaller, uriRedirectListener, new AccessTokenCallback() { @Override public void onError(CloudRailException arg0) { // TODO Auto-generated method stub } @Override public void onToken(String arg0) { // TODO Auto-generated method stub // How to retrieve the access token with CloudRail cloudDownload.dropboxDL(dropboxCloudFilePath, "retrievedAccessToken", new DropboxDLResponseListener() { @Override public void onSuccess(InputStream file) { // TODO Auto-generated method stub // what I need to print out simple Hello work in console System.out.println("Hello"); } @Override public void onProgress(double percentFinished) { // TODO Auto-generated method stub } @Override public void onError(CloudRailException error) { // TODO Auto-generated method stub error.printStackTrace(); } }); } }); cloudDownload.start_DropboxContentAPI_Auth(); } }

1 个答案:

答案 0 :(得分:2)

对于您的第一个问题:CloudRail实际上可以为您提供验证的可能性,您的代码已经非常接近了。 在用户成功通过身份验证后,您提供的AccessTokenCallback类将在其onToken方法中接收访问令牌作为参数。这意味着您必须按如下方式更改代码:

-Lang.Php.dll
-Lang.Php.pdb
-Lang.Php.Wp.dll
-Lang.Php.Wp.pdb

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

// Create the email and send the message
$to = 'yourname@yourdomain.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

对于第二个问题,OAuth 2.0的重定向URI是身份验证成功后身份验证服务器重定向到的URI。它通常是指向服务器上特殊端点的URL。对于在本地运行的简单控制台应用程序,只需重定向到localhost即可。由于您的URIRedirectListener设置服务器以在端口8080上侦听此类重定向,因此您必须放置

https://localhost:8080

作为重定向URI。

为了解决你的第三点,假设你修复了另外两件事,这应该按原样运作。

在尝试代码之前,您必须记住为客户端标识符和机密设置有效值。