运用CloudRail实现隐式链接上传文件到oneDrive,googleDrive

 

CloudRail是一款第三方SDK,使用他可以很方便的实现统一云存(Box,Dropbox,Google Drive, OneDrive...),统一邮件发送,统一支付,统一社交等等,下面介绍如何使用CloudRail实现免登陆操作OneDrive,GooleDrive.

1.在你的Android Studio中添加CloudRail的依赖:

compile 'com.cloudrail:cloudrail-si-android:2.22.2    

 

2.初始化OneDrive

 

private final AtomicReference<CloudStorage> onedrive = new AtomicReference<>();

 

3.创建OneDrive对象,Client ID和Client Secret获得方法在后面介绍:

new OneDrive(context, "[Client ID]", "[Client Secret]");

Googledrive对象的new不需要Client secret,因为google官方的设定,无法从内置浏览器中登陆网盘,所以需要跳转到浏览器登陆,做隐式登陆后无需进行此操作.

/**
 * @param context Provides a context to enable OAuth authorization flows, often 'this' can be used when within an Activity
 * @param clientIdentifier The client identifier of your Google app
 * @param clientSecret The client secret of your Google app, must be an empty string on this platform
 * @param redirectUri The redirect URI. Must be your package name / bundle ID and any path you like
 * @param state The state which can be used to prevent CSRF or transfer additional information about a user
 */
GoogleDrive(
        Context context,
        String clientIdentifier,
        String clientSecret,
        String redirectUri,
        String state
);

--------------------------------------------------------------

new GoogleDrive(context,
"client id",//google api官网获取的凭证
        "",
        "com.cloudrail.fileviewer:/oauth2redirect",//冒号前面为项目包名,需要在google api上注册,冒号和后面为固定格式.为了挑战到外部浏览器后挑战回你自己的应用
        "")

4.Onedrive对象set后可对其进行网盘操作:

onedrive.set(new OneDrive(context, "client id", "client secret"));

(到此为止已经集成了cloudrail环境,但是还不是隐式登陆,需要输入相应的账号密码才可以对onedrive进行操作,下面介绍如何获取隐式登陆token)

Log.i(TAG, "OneDrive token: " + onedrive.get().saveAsString());

(使用saveAsString()方法可获得onedrive的账号信息,做一次测试应用,在登陆账号后获得账号的token信息,保存账号信息,修改代码,在new对象后加载账号信息,可实现不登陆账号密码直接对账号进行操作)

InputStream is = context.getResources().openRawResource(R.raw.onedrive);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
    OneDrive oneDrive = new OneDrive(context, "client id", "client secret");
    oneDrive.loadAsString(line);
    onedrive.set(oneDrive);
}
is.close();

5.网盘上传操作upload(上传文件的路径,文件内容的流,文件的大小,是否进行覆盖);一般都进行同文件名进行覆盖,false如出现同文件名则抛出异常.

/**
 * @param path The destination path for the new file
 * @param stream The file content that will be uploaded
 * @param size The file size measured in bytes
 * @param overwrite Indicates if the file should be overwritten. Throws an error if set to false and the specified file already exists
 * @throws IllegalArgumentException Null or malformatted path, null stream or negative file size
 */
void upload(
        String path,
        InputStream stream,
        Long size,
        Boolean overwrite
);

6.传入文件路径后可创建共享连接,返回字符串,谁拿到连接都可以获得分享的内容(危险操作)

/**
 * @param path The path to the file or folder to share
 * @return The shareable link
 * @throws IllegalArgumentException Attempt to share root
 */
String createShareLink(
        String path
);

7.文件下载download(共享连接);返回以流的形式的文件内容,对其进行保存到本地操作.

public String download(String cloudFilePath) {
    String tar_path = null;
    try {
        CloudStorage cs = getTargetedCloudStorageByUrl(cloudFilePath);//cs 只是网盘对象
        InputStream is = cs.download(cloudFilePath);
        long size = is.available();
        File f = new File(context.getFilesDir(), "downloaded_" + UriUtils.getFileNameFromUrl(cloudFilePath));
        String outpath = f.getPath();
        FileOutputStream fos = new FileOutputStream(outpath);
        byte[] b = new byte[1024];
        while ((is.read(b)) != -1) {
            fos.write(b);
        }
        is.close();
        fos.close();
        tar_path = f.getAbsolutePath();
    } catch (FileNotFoundException e) {
        errorMsg = e.getMessage();
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        errorMsg = e.getMessage();
        e.printStackTrace();
        return null;
    } catch (Exception e) {
        errorMsg = e.getMessage();
        e.printStackTrace();
        return null;
    }
    return tar_path;
}

更多方法参考官方网站https://cloudrail.com/

介绍如何获得goog的client id(参考cloudrail单个集成的设置说明)

  1.  搜索google api或者其他方式进入到google开发者中心,申请开发者账号后创建一个新的项目:https//console.developers.google.com/iam-admin/projects
  2. 点击创建项目,填写创建项目的名称.
  3. 在搜索栏中输入“drive”,选择“Google Drive API”,然后选择“启用”
  4. 单击“转到凭据”按钮
  5. 选择“创建凭据” - >“OAuth客户端ID”
  6. 选择“Android”
  7. 输入SHA-1签名证书指纹
  8. keytool -exportcert -keystore /home/xing/Android/FileSender2.jks -list -v
  9. 其中/home/xing/Android/FileSender2.jks为你签名证书的路径,回车后输入su密码,获得sha-1指纹
  10. 同时输入manifest中显示的文件包名,用于重定向URI.
  11. 点击创建
  12. 现在我们应该可以看到我们的client id

意:对于这种类型的应用程序,没有客户端的秘密,当使用SDK时,您需要传递一个空字符串作为秘密!

介绍如何从Microsoft获得我们的client id和client secret(参考cloudrail单个集成的设置说明)

  1. 搜索onedrive api或者其他方式进入Microsoft的onedrive开发者中心,申请开发者账号后创建一个新的应用程序:https//apps.dev.microsoft.com/#/appList
  2. .点击添加应用,填写应用名称.
  3. .在这个应用程序界面,我们可以编辑我们的应用程序,也可以找到我们需要的client id 和client secret,但是需要一下操作才可以正常使用.
  4. .点击“添加平台”按钮,然后选择“网络”
  5. .将重定向URI设置为“https://www.cloudrailauth.com/auth”
  6. .勾选页面底部“高级选项”下的“实时SDK支持”
  7. .点击“保存”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值