LoginSignup
1
0

More than 1 year has passed since last update.

Salesforce でBox APIを使う

Last updated at Posted at 2022-05-24

まとめページに戻る
まとめA~M

ログインページ

思ったより難しいので、考えをまとめるために記録しておきます。1日では解決できません。何を調べたかを忘れそうです。

元の質問 : BoxにAPI接続がしたい

BOx APIのリファレンス : ファイルバージョンをアップロード

CURLの例ではこうなっているが、Json形式ではどうなるのかな? 特にファイルの指定が理解できん。Linuxなら Bulk APIの書き方と同じようです。
この3つのフィールドが必須みたいなのは、理解できるがパラメータの表示の仕方が分からない。

curl -i -X POST "https://upload.box.com/api/2.0/files/12345/content" \
     -H "Authorization: Bearer <ACCESS_TOKEN>" \
     -H "Content-Type: multipart/form-data" \
     -F attributes='{"name":"Contract.pdf", "parent":{"id":"11446498"}}' \
     -F file=@<FILE_NAME>

image.png

CURLの例からこんなJsonをイメージしたけど ファイルのところが分からない。

"data":{"attributes":{"name":"testFile","parent":{"id":"0"}},"file":"mary had a little lamb."}}

更にファイルの関係を調べてみると

両方のプラットフォームが異なる形式のファイルを処理することがわかりました。Salesforceは通常、データをbase64形式で保存しますが、BOX.netはそれらをバイナリとして想定しています。そのため、ファイルをボックスにアップロードしている間、ファイルが破損し、ブラウザを使用してbox.netにアクセスすると、ファイルを表示できなくなります。

えええ、バイナリかい。

おまけに Saesforceからはバイナリは送信できないことが書かれている。ああ、詰んだな。

最終的にはこれを使わないと難しいかもと思って、ソースコードを覗いてみると
https://github.com/box/box-salesforce-sdk 

無いんですね、Fileのアップロード機能は... 代わりと言えば既にBox上にあるファイルについては操作できそう。BoxFileというオブジェクトにオプションがあります。

UploadというのがついたオブジェクトはBoxUploadEmail があります。 もしかしてメール経由でアップする?

メールを使用したBoxへのアップロード

しかし、更に調べてみると

Apexを使用したSalesforceとBoxの統合

apexクラスでは、BLOBをBoxに直接アップロードできないため、これをバイナリデータに変換して、BoxStorageにアップロードします。

この部分が欲しいものかもしれない。

//code to upload files to BOX.COM
    @AuraEnabled
   public static String uploadFile(String base64, String filename, String folderId){
        String accessToken = getAccessToken();
        if(base64!=null){
            blob base64EncodeFile = base64EncodeFileContent(EncodingUtil.base64Decode(base64), filename);
            String uploadEndPointURL='https://upload.box.com/api/2.0/files/content?parent_id='+folderId;
            String boundary = '----------------------------741e90d31eff';
            String body = '{"name":"' + filename + '", "parent":{"id":"' + folderId + '"}}';
            HttpRequest req = new HttpRequest();
            req.setBody(body);
            req.setBodyAsBlob(base64EncodeFile);
            req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
            //req.setHeader('Content-Length',String.valueof(req.getBodyAsBlob().size()));
            req.setHeader('Authorization', 'Bearer ' + accessToken);
            req.setMethod('POST');
            req.setEndpoint(uploadEndPointURL);
            req.setTimeout(120000);
            //Send request to Box
            Http ht = new Http();
            HTTPResponse res = ht.send(req);
            
            System.debug('**Files upload Response: ' + res.getBody());
            Integer uploadStatusCode=res.getStatusCode();
            if(uploadStatusCode==201){
                return 'File uploaded successfully.';
            }else{
              throw newMessageException('Error encountered. Status Code: ' + uploadStatusCode);                
            }
        }else{
            throw newMessageException('Please select file.');
        }
   }

関連しそうな資料

質問

Box for Salesfroce

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0