box-api https://www.e-learn.cn/tag/box-api zh-hans Connecting via oAuth2 with jQuery/AJAX for Box.com https://www.e-learn.cn/topic/3671158 <span>Connecting via oAuth2 with jQuery/AJAX for Box.com</span> <span><span lang="" about="/user/27" typeof="schema:Person" property="schema:name" datatype="">我只是一个虾纸丫</span></span> <span>2020-06-23 14:48:37</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'm trying to work with Box.com's API to develop a quick app that allows for Folder creation. I am having trouble connecting to their API and am fairly new to oAUTH2, API's, and whatnot. I've tried to follow these guides:</p> <p>http://developers.box.com/oauth/</p> <p>http://developers.box.com/docs/#folders-create-a-new-folder</p> <p>The Box.com documentation says</p> <blockquote> <p>response_type: Whether the endpoint returns an authorization code. For web applications, a value of code should be used.</p> <p>client_id : The client_id you obtained in the Initial Setup.</p> <p>redirect_uri :An HTTPS URI or custom URL scheme where the response will be redirected. Optional if the redirect URI is registered with Box already. </p> <p>state : An arbitrary string of your choosing that will be included in the response to your application. Box recommends that you use an anti-forgery state token to prevent CSRF attacks to your users</p> <p>A sample GET request could therefore look like:</p> <p>GET https: //www.box.com/api/oauth2/authorize?response_type=code&amp;client_id=MY_CLIENT_ID&amp;state=security_token%3DKnhMJatFipTAnM0nHlZA</p> </blockquote> <p>I have a dev account with them and here is my basic jquery that is not working..</p> <pre><code> $.ajax({ //The URL to process the request url : 'https://www.box.com/api/oauth2/authorize', type : 'GET', data : { response_type : 'code', client_id : 'm025a55gtov17txux1v2vbzjjhph2b6n' }, success: function( resp ) { console.log( resp.people ); }, error: function( req, status, err ) { console.log( 'something went wrong', status, err );} }); </code></pre> <p>Can anyone point me in the direction on how to do this? I'm stumped.</p> <br /><h3>回答1:</h3><br /><p>I did find a way to connect to their API and get a token, but now I am getting a CORS error when I try and send a POST request to their server to create a folder (the main goal of my app) for anyone interested.. here is how I trade off the code for the token</p> <pre><code>authorizeUser = function(){ var results = $.ajax({ // The URL to process the request url : 'https://www.box.com/api/oauth2/token', type : 'POST', data : { grant_type : 'authorization_code', code : data.boxAuthorizationCode, client_id : data.clientId, client_secret : data.clientSecret }, beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", "Bearer $token") }, dataType: "json", success: function(response) { //console.log(response); console.log(response.access_token); data.access_token = response.access_token; tokenGranted(); } }); return results.responseText; }, </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>You can use Axios instead of ajax</p> <pre><code>&lt;script src="https://unpkg.com/axios/dist/axios.min.js"&gt;&lt;/script&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/qs/6.9.4/qs.js"&gt;&lt;/script&gt; </code></pre> <p>After requiring this script you can write your own javascript function and it will work.</p> <p>clientId: Pass dynamic client id. secret: Pass dynamic secret.</p> <pre><code> async function runAuthQuery(params) { const config = { url: '{WebURL}', method: 'post', data: Qs.stringify({ grant_type: 'client_credentials', client_id: clientId, client_secret: secret, }) }; const bearerToken = await axios(config); const getPlayableUrl = await axios.get(`{WebURL}`, { "headers": { "content-type": "application/x-www-form-urlencoded", "authorization": `Bearer ${bearerToken.data.access_token}` } }); } </code></pre> <p>It is working fine for me.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/23482705/connecting-via-oauth2-with-jquery-ajax-for-box-com</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/jquery" hreflang="zh-hans">jquery</a></div> <div class="field--item"><a href="/tag/ajax" hreflang="zh-hans">ajax</a></div> <div class="field--item"><a href="/tag/oauth-20" hreflang="zh-hans">oauth-2.0</a></div> <div class="field--item"><a href="/tag/box-api" hreflang="zh-hans">box-api</a></div> </div> </div> Tue, 23 Jun 2020 06:48:37 +0000 我只是一个虾纸丫 3671158 at https://www.e-learn.cn upload file to box api v2 https://www.e-learn.cn/topic/3371032 <span>upload file to box api v2</span> <span><span lang="" about="/user/233" typeof="schema:Person" property="schema:name" datatype="">五迷三道</span></span> <span>2020-02-07 08:24:07</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>i am trying to upload a file to box.com with their v2 api. i am able to successfully upload a file with curl, but cannot upload a file from my rails application. i am passing my upload function the correct folder id and file is a tempfile object created by a form upload in my app.</p> <p>here is the successful curl command</p> <pre><code> curl https://upload.box.com/api/2.0/files/data -H "Authorization: BoxAuth api_key=API_KEY&amp;auth_token=TOKEN" -F contract=@test.png -F folder_id=387656851 -ssl3 </code></pre> <p>and here is my ruby code</p> <pre><code>class BoxApi require 'httmultiparty' include HTTMultiParty ssl_version :SSLv3 def initialize @key = API_KEY @token = TOKEN end def upload_file(folder_id,file,filename,content_type) File.open(file) do |open_file| response = self.class.post('https://upload.box.com/2.0/files/data', :query =&gt; { :file =&gt; open_file, :folder_id =&gt; folder_id }, :headers =&gt; {'Authorization' =&gt; "BoxAuth api_key=#{@key}&amp;auth_token=#{@token}"}) p response end </code></pre> <p>end</p> <p>i get an html page back from box with this text It appears that your firewall may be blocking Box or you are encountering an error.<br /><br />Please contact your IT administrator to configure your firewall to recognize all sub-domains of .box.com, .box.com and .boxcdn.net. The ports that should be opened for these domains are 80 and 443.<br /><br />If that does not resolve the issue, then please submit a support ticket at https://www.box.com/help. </p> <p>any ideas why the curl command would be working but not the ruby code?</p> <br /><h3>回答1:</h3><br /><p>This works properly for me</p> <pre><code>require 'httmultiparty' class SomeClient include HTTMultiParty base_uri 'https://api.box.com/2.0' end response = SomeClient.post('/files/data', :headers =&gt; { 'authorization' =&gt; 'BoxAuth api_key={YOUR API KEY}&amp;auth_token={YOUR TOKEN' }, :body =&gt; { :folder_id =&gt; '0', :somefile =&gt; File.new('large.jpeg')} ) </code></pre> <p>I would try to verify that</p> <ol><li>You can make non-upload API calls (i.e. GET /folders/0)</li> <li>If not, check your firewall settings.</li> </ol><br /><br /><br /><h3>回答2:</h3><br /><p>Despite from being late, this could be useful for people who came across this question. There is a gem ruby-box to use with Box service at the 2.0 version of their API.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>Sean already covered this in his answer but I'll highlight it explicitly. We had some issues using the https://upload.box.com URL which is no longer recommended by box. I'd recommend trying the https://api.box.com/2.0 URL and seeing if that it changes your results.</p> <p>Worst case I'd try capturing my packets using a packet analyzer like wireshark and looking for differences between the two cases.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/12394894/upload-file-to-box-api-v2</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/ruby" hreflang="zh-hans">ruby</a></div> <div class="field--item"><a href="/tag/file-upload" hreflang="zh-hans">file-upload</a></div> <div class="field--item"><a href="/tag/box-api" hreflang="zh-hans">box-api</a></div> <div class="field--item"><a href="/tag/httparty" hreflang="zh-hans">httparty</a></div> </div> </div> Fri, 07 Feb 2020 00:24:07 +0000 五迷三道 3371032 at https://www.e-learn.cn Get all files in box account https://www.e-learn.cn/topic/3300709 <span>Get all files in box account</span> <span><span lang="" about="/user/9" typeof="schema:Person" property="schema:name" datatype="">爷,独闯天下</span></span> <span>2020-01-24 20:49:25</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I need to fetch a list of all the files in a user's box account, such that the list of files can then be displayed in a table view (iOS).</p> <p>I have successfully implemented this by recursively using /folders/{folder id}/items on all the folder's in my user's box.</p> <p>However, while this works, it's kind of dirty, seeing as how a request is made for each of the users's folders, which could be quite a large number.</p> <p>Is there any way to get a list of all the files (it's no issue if folders are included, I can ignore those manually) available?</p> <p>I tried implementing this using search, but I couldn't identify a value for the query parameter that returned everything.</p> <p>Any help would be appreciated.</p> <blockquote> <p>Help me, Obi-Wan Kenobi. You're my only hope.</p> </blockquote> <br /><h3>回答1:</h3><br /><p>What you are looking for (recursive call through a Box account) is not available. We have enterprise customers will bajillions of files and millions of folders. Recursively asking for everything would take too long. </p> <p>What we generally recommend is that you ask for as little as you can, and that you use multiple threads and anticipate what you'll need just a little bit, so that you can deliver a high-performance user-interface to your end-users. </p> <p>For example ?fields=item_collection is expensive to retrieve, and can add a lot to a paylaod. It can double, or 10x the time that it takes to get back a payload from the Box API. Most UI's don't need to show all the items inside every folder. So they are better off asking for ?fields=.</p> <p>You can make your application responsive to the user if you make the smallest possible call. Of course there is a balance. Mobile networks have high latency, and sometimes that next API call to show some extra thing is slow. But for a folder tree, you can get high performance by retrieving only the current level, displaying that, and then starting to fetch one-level down while the user is looking at the first level. </p> <p>Same goes for displaying thumbnails. If a user drills into a folder and starts looking at thumbnails for pictures, there's a good chance they'll want to see other thumbnails in that same folder. Your app should anticipate that, and start to pull one or two extras down in the background. Yes, it means more API calls, but your users will give your app a higher rating for being fast. </p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/26107389/get-all-files-in-box-account</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/box-api" hreflang="zh-hans">box-api</a></div> </div> </div> Fri, 24 Jan 2020 12:49:25 +0000 爷,独闯天下 3300709 at https://www.e-learn.cn getting file size for box files retrieved via box search https://www.e-learn.cn/topic/3232461 <span>getting file size for box files retrieved via box search</span> <span><span lang="" about="/user/51" typeof="schema:Person" property="schema:name" datatype="">大兔子大兔子</span></span> <span>2020-01-16 00:54:10</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>My code is as follows:</p> <pre><code> requestObj.addField(BoxFolder.FIELD_SIZE); BoxCollection itemCollection = client.getFoldersManager().getFolderItems(file.getId(), (BoxFolderRequestObject) requestObj); List&lt;BoxTypedObject&gt; objects = itemCollection.getEntries(); for (BoxTypedObject innerFile : objects) { System.out.println("=============" + innerFile.getExtraData(BoxFolder.FIELD_SIZE)); System.out.println("=============" + innerFile.getValue(BoxFolder.FIELD_SIZE)); } </code></pre> <p>It returns null in both System.out</p> <p>I looked at BoxTypedObject which does not have a way to return file size. </p> <p>I do see getSize() in BoxItem but that is not returned by <code>Box.getFolderItems ---&gt; BoxCollection ----&gt; BoxTypedObject</code></p> <p>Please provide a way to get Box file size</p> <p>More Context:</p> <p>https://github.com/box/box-java-sdk-v2/issues/58</p> <br /><h3>回答1:</h3><br /><p>BoxTypedObject can be various types. You could do this: if (innerFile instanceof BoxItem) { size = ((BoxItem) innerFile).getSize(); }</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/20616200/getting-file-size-for-box-files-retrieved-via-box-search</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/box-api" hreflang="zh-hans">box-api</a></div> </div> </div> Wed, 15 Jan 2020 16:54:10 +0000 大兔子大兔子 3232461 at https://www.e-learn.cn console based application using box.com outh with out asking user to login and allow the app https://www.e-learn.cn/topic/3221946 <span>console based application using box.com outh with out asking user to login and allow the app</span> <span><span lang="" about="/user/167" typeof="schema:Person" property="schema:name" datatype="">半世苍凉</span></span> <span>2020-01-15 04:56:05</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Here is my situation. I am developing a console based application using c#. Here we can not have user login. Only i can do is using api i need to get auth_code then access token and refresh token. I am very happy if somebody can answer, how to implement this in my console app. i mean how to get the auth_code with out asking user to login and allow the application.</p> <p>Here if i run a cURL command i am getting html response, which is the complete html for box login page.</p> <p>Regards, Pradeep</p> <br /><h3>回答1:</h3><br /><p>The current OAuth 2 flow requires the user to go through the browser and can't be done programmatically.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/15907076/console-based-application-using-box-com-outh-with-out-asking-user-to-login-and-a</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/box-api" hreflang="zh-hans">box-api</a></div> </div> </div> Tue, 14 Jan 2020 20:56:05 +0000 半世苍凉 3221946 at https://www.e-learn.cn API 2.0 how to upload file with POSTMAN? https://www.e-learn.cn/topic/3209229 <span>API 2.0 how to upload file with POSTMAN?</span> <span><span lang="" about="/user/200" typeof="schema:Person" property="schema:name" datatype="">余生长醉</span></span> <span>2020-01-14 04:41:26</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'm reading API 2.0 documentation and try using POSTMAN (recommended in the documentation).</p> <p>In "Upload a file" section, I got "404 not found" error.</p> <p>for example:</p> <p>{"type":"error","status":404,"code":"not_found","help_url":"","message":"Unknown Error","request_id":"11139828924fa91c0d283d2"}</p> <p>POSTMAN window image is here. Base URL is tring "upload.box.com/api/2.0" (temporary, according to the document).</p> <p>What's wrong?</p> <br /><h3>回答1:</h3><br /><p>The domain specific Upload issues have all been resolved. You can now direct all API requests, including uploads, to https://api.box.com/2.0</p> <br /><br /><br /><h3>回答2:</h3><br /><p>One more point, your POSTMAN request misses to indicate <code>folder_id</code> in its form data . You need that "<code>folder_id</code>" value for the request to work .</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/10500241/api-2-0-how-to-upload-file-with-postman</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/box-api" hreflang="zh-hans">box-api</a></div> </div> </div> Mon, 13 Jan 2020 20:41:26 +0000 余生长醉 3209229 at https://www.e-learn.cn Best way to upload files to Box.com programmatically https://www.e-learn.cn/topic/3200480 <span>Best way to upload files to Box.com programmatically</span> <span><span lang="" about="/user/79" typeof="schema:Person" property="schema:name" datatype="">你离开我真会死。</span></span> <span>2020-01-13 10:44:53</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I've read the whole Box.com developers api guide and spent hours on the web researching this particular question but I can't seem to find a definitive answer and I don't want to start creating a solution if I'm going down the wrong path. We have a production environment where as once we are finished working with files our production software system zips them up and saves them into a local server directory for archival purposes. This local path cannot be changed. My question is how can I programmatically upload these files to our Box.com account so we can archive these on the cloud? Everything I've read regarding this involves using OAuth2 to gain access to our account which I understand but it also requires the user to login. Since this is an internal process that is NOT exposed to outside users I want to be able to automate this otherwise it would not be feasable for us. I have no issues creating the programs to trigger everytime a new files gets saved all I need is to streamline the Box.com access.</p> <br /><h3>回答1:</h3><br /><p>I just went through the exact same set of questions and found out that currently you CANNOT bypass the OAuth process. However, their refresh token is now valid for 60 days which should make any custom setup a bit more sturdy. I still think, though, that having to use OAuth for an Enterprise setup is a very brittle implementation -- for the exact reason you stated: it's not feasible for some middleware application to have to rely on an OAuth authentication process.</p> <h2>My Solution:</h2> <p>Here's what I came up with. The following are the same steps as outlined in various box API docs and videos:</p> <ol><li>use this URL https://www.box.com/api/oauth2/authorize?response_type=code&amp;client_id=[YOUR_CLIENT_ID]&amp;state=[box-generated_state_security_token] (go to https://developers.box.com/oauth/ to find the original one)</li> <li>paste that URL into the browser and GO</li> <li>authenticate and grant access</li> <li>grab the resulting URL: http://0.0.0.0/?state=[box-generated_state_security_token]&amp;code=[SOME_CODE] and note the "code=" value.</li> <li>open POSTMAN or Fiddler (or some other HTTP sniffer) and enter the following:<br /><ul><li>URL: https://www.box.com/api/oauth2/token<br /></li> <li>create URL encoded post data:<br /><ul><li>grant_type=authorization_code<br /></li> <li>client_id=[YOUR CLIENT ID]<br /></li> <li>client_secret=[YOUR CLIENT SECRET]<br /></li> <li>code= &lt; enter the code from step 4 &gt;<br /></li> </ul></li> </ul></li> <li>send the request and retrieve the resulting JSON data:<pre> { "access_token": "[YOUR SHINY NEW ACCESS TOKEN]", "expires_in": 4255, "restricted_to": [], "refresh_token": "[YOUR HELPFUL REFRESH TOKEN]", "token_type": "bearer" } </pre></li> </ol><p>In my application I save both auth token and refresh token in a format where I can easily go and replace them if something goes awry down the road. Then, I check my authentication each time I call into the API. If I get an authorization exception back I refresh my token programmatically, which you <em>can</em> do! Using the BoxApi.V2 .NET SDK this happens like so:</p> <pre><code>var authenticator = new TokenProvider(_clientId, _clientSecret); // calling the 'RefreshAccessToken' method in the SDK var newAuthToken = authenticator.RefreshAccessToken([YOUR EXISTING REFRESH TOKEN]); // write the new token back to my data store. Save(newAuthToken); </code></pre> <p>Hope this helped!</p> <br /><br /><br /><h3>回答2:</h3><br /><p>If I understand correctly you want the entire process to be automated so it would not require a user login (i.e run a script and the file is uploaded). Well, it is possible. I am a rookie developer so excuse me if I'm not using the correct terms.</p> <p>Anyway, this can be accomplished by using cURL. First you need to define some variables, your user credentials (username and password), your client id and client secret given by Box (found in your app), your redirect URI and state (used for extra safety if I understand correctly).</p> <p>The <code>oAuth2.0</code> is a 4 step authentication process and you're going to need to go through each step individually.</p> <p>The first step would be setting a curl instance:</p> <pre><code>curl_setopt_array($curl, array( CURLOPT_URL =&gt; "https://app.box.com/api/oauth2/authorize", CURLOPT_RETURNTRANSFER =&gt; true, CURLOPT_ENCODING =&gt; "content-type: application/x-www-form-urlencoded", CURLOPT_MAXREDIRS =&gt; 10, CURLOPT_TIMEOUT =&gt; 30, CURLOPT_HTTP_VERSION =&gt; CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST =&gt; "POST", CURLOPT_POSTFIELDS =&gt; "response_type=code&amp;client_id=".$CLIENT_ID."&amp;state=".$STATE, )); </code></pre> <p>This will return an html text with a request token, you will need it for the next step so I would save the entire output to a variable and grep the tag with the request token (the tag has a <code>"name" = "request_token"</code> and a <code>"value"</code> which is the actual token).</p> <p>Next step you will need to send another curl request to the same url, this time the post fields should include the request token, user name and password as follows:</p> <pre><code>CURLOPT_POSTFIELDS =&gt; "response_type=code&amp;client_id=".$CLIENT_ID."&amp;state=".$STATE."&amp;request_token=".$REQ_TOKEN."&amp;login=".$USER_LOGIN."&amp;password=".$PASSWORD </code></pre> <p>At this point you should also set a cookie file:</p> <pre><code> CURLOPT_COOKIEFILE =&gt; $COOKIE, (where $COOKIE is the path to the cookie file) </code></pre> <p>This will return another html text output, use the same method to grep the token which has the name "ic".</p> <p>For the next step you're going to need to send a post request to the same url. It should include the postfields:</p> <pre><code>response_type=code&amp;client_id=".$CLIENT_ID."&amp;state=".$STATE."&amp;redirect_uri=".$REDIRECT_URI."&amp;doconsent=doconsent&amp;scope=root_readwrite&amp;ic=".$IC </code></pre> <p>Be sure to set the curl request to use the cookie file you set earlier like this:</p> <pre><code>CURLOPT_COOKIEFILE =&gt; $COOKIE, </code></pre> <p>and include the header in the request:</p> <pre><code>CURLOPT_HEADER =&gt; true, </code></pre> <p>At step (if done by browser) you will be redirected to a URL which looks as described above:</p> <pre><code>http://0.0.0.0(*redirect uri*)/?state=[box-generated_state_security_token]&amp;code=[SOME_CODE] and note the "code=" value. </code></pre> <p>Grab the value of <code>"code"</code>.</p> <p>Final step!</p> <p>send a new cur request to https//app.box.com/api/oauth2/token This should include fields:</p> <pre><code>CURLOPT_POSTFIELDS =&gt; "grant_type=authorization_code&amp;code=".$CODE."&amp;client_id=".$CLIENT_ID."&amp;client_secret=".$CLIENT_SECRET, </code></pre> <p>This will return a string containing "access token", "Expiration" and "Refresh token". These are the tokens needed for the upload. read about the use of them here: https://box-content.readme.io/reference#upload-a-file</p> <p>Hope this is somewhat helpful. P.S, I separated the https on purpuse (Stackoverflow wont let me post an answer with more than 1 url :D) this is for PHP cURL. It is also possible to do the same using Bash cURL. </p> <br /><br /><br /><h3>回答3:</h3><br /><p>Have you thought about creating a box 'integration' user for this particular purpose. It seems like uploads have to be made with a Box account. It sounds like you are trying to do an anonymous upload. I think box, like most services, including stackoverflow don't want anonymous uploads. </p> <p>You could create a system user. Go do the Oauth2 dance and store just the refresh token somewhere safe. Then as the first step of your script waking up go use the refresh token and store the new refresh token. Then upload all your files. </p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/20252226/best-way-to-upload-files-to-box-com-programmatically</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/upload" hreflang="zh-hans">upload</a></div> <div class="field--item"><a href="/tag/oauth-20" hreflang="zh-hans">oauth-2.0</a></div> <div class="field--item"><a href="/tag/box-api" hreflang="zh-hans">box-api</a></div> </div> </div> Mon, 13 Jan 2020 02:44:53 +0000 你离开我真会死。 3200480 at https://www.e-learn.cn how to send form data in a programmatic file upload in box api 2.0 https://www.e-learn.cn/topic/3134376 <span>how to send form data in a programmatic file upload in box api 2.0</span> <span><span lang="" about="/user/117" typeof="schema:Person" property="schema:name" datatype="">℡╲_俬逩灬.</span></span> <span>2020-01-06 15:42:33</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Is the following a correct example of the form POST data in a file upload in the box api 2.0? The documentation says that the 'filename' form field is a string but when sending the post data in, say, python, you need to actually send the file contents in the post. So, is the content-disposition: form-data line below the correct way to name the 'filename' field and include the file contents?</p> <pre><code>Content-type: multipart/form-data, boundary=AaB03x Content-length: 142 Authorization: BoxAuth api_key=MY_API_KEY&amp;auth_token=MY_AUTH_TOKEN --AaB03x content-disposition: form-data; name="filename"; filename="test.txt" Content-type: text/plain testing box api 2.0 --AaB03x-- </code></pre> <br /><h3>回答1:</h3><br /><p>Yes, that is the correct way to do it.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>You mention Python, although you don't mention as being a requirement. If you can use Ruby (another scripting language) you have a very nice lib to deal with Box API at the 2.0 version.</p> <p>The lib is named ruby-box and in the readme you can find how to use it.</p> <br /><br /><br /><h3>回答3:</h3><br /><pre><code>-------boundary Content-Disposition: form-data; name="filename"; filename="82b.gif" Content-Type: image/gif Content-Transfer-Encoding: BASE64 $base64_encoded_binary_file_content -------boundary Content-Disposition: form-data; name="parent_id" 123456789 -------boundary-- </code></pre> <p>This is working code.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/10372708/how-to-send-form-data-in-a-programmatic-file-upload-in-box-api-2-0</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/box-api" hreflang="zh-hans">box-api</a></div> </div> </div> Mon, 06 Jan 2020 07:42:33 +0000 ℡╲_俬逩灬. 3134376 at https://www.e-learn.cn BOX Access token API V2 https://www.e-learn.cn/topic/3127821 <span>BOX Access token API V2</span> <span><span lang="" about="/user/126" typeof="schema:Person" property="schema:name" datatype="">情到浓时终转凉″</span></span> <span>2020-01-06 08:11:36</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am trying to retrieve an Access Token from BOX.COM oauth2 and no matter what I do I get the same error. {"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}. I have verified the client id, client secret and get a new code every time I try (the expire every 30 seconds). I have tried VB.NET, C# and even the RestClient plugin for FireFox. Below is the VB.NET code that I am using. Any help would be greatly appreciated! Thanks, Brian</p> <pre><code>Public Sub GetAccessToken(ByVal code As String, ByVal client_id As String, ByVal client_secret As String) Dim xrc As RestClient = New RestClient Dim grant_type As String = "authorization_code" Dim request As New RestRequest(Method.POST) Dim strHeaders As String Dim response As RestResponse Dim strResponse As String Try 'Base URL xrc.BaseUrl = "https://api.box.com" 'Resource request.Resource = "oauth2/token" 'Format Headers strHeaders = String.Format("grant_type={0}&amp;code={1}&amp;client_id={2}&amp;client_secret={3}", grant_type, code, client_id, client_secret) 'Add Headers to request request.AddHeader("Authorization", strHeaders) 'Execute response = xrc.Execute(request) 'Parse Response strResponse = response.Content Catch ex As Exception End Try End Sub </code></pre> <br /><h3>回答1:</h3><br /><p>This is the problem</p> <pre><code> 'Format Headers strHeaders = String.Format("grant_type={0}&amp;code={1}&amp;client_id={2}&amp;client_secret={3}", grant_type, code, client_id, client_secret) 'Add Headers to request request.AddHeader("Authorization", strHeaders) </code></pre> <p>You need to send that string as part of the POST body, not as headers.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>You may also need to add the following to ensure you're using xml encoding.</p> <pre><code>request.Method = Method.POST request.RequestFormat = DataFormat.Xml </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/14528799/box-access-token-api-v2</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/net" hreflang="zh-hans">.net</a></div> <div class="field--item"><a href="/tag/vbnet" hreflang="zh-hans">vb.net</a></div> <div class="field--item"><a href="/tag/api" hreflang="zh-hans">api</a></div> <div class="field--item"><a href="/tag/token" hreflang="zh-hans">token</a></div> <div class="field--item"><a href="/tag/box-api" hreflang="zh-hans">box-api</a></div> </div> </div> Mon, 06 Jan 2020 00:11:36 +0000 情到浓时终转凉″ 3127821 at https://www.e-learn.cn How to send curl update request with -d parameter? https://www.e-learn.cn/topic/3123960 <span>How to send curl update request with -d parameter?</span> <span><span lang="" about="/user/230" typeof="schema:Person" property="schema:name" datatype="">不问归期</span></span> <span>2020-01-06 06:00:27</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I need to send a curl request from powershell, using box api reference for help (I'm looking the the section called <code>Update User</code>, but I'm having some trouble:</p> <pre><code>curl https://api.box.com/2.0/users/11111 -H @{"Authorization" = "token"} -d '{"name": "bob"}' -X PUT </code></pre> <p>Should update the user's name, but I get:</p> <blockquote> <p>Invoke-WebRequest : A positional parameter cannot be found that accepts argument '{"name": "bob"}'. At G:\IT\bassie\Box\GetUsers.ps1:5 char:1 + curl https://api.box.com/2.0/users/892861590 -H @{"Authorization" = " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand</p> </blockquote> <p>I tried re-arranging it to </p> <pre><code>-d @{"name" = "bob"} </code></pre> <p>but the error changed to </p> <blockquote> <p>Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'. At G:\IT\bassie\Box\GetUsers.ps1:5 char:1 + curl https://api.box.com/2.0/users/892861590 -H @{"Authorization" = " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand</p> </blockquote> <p>What do I need to put into the <code>-d</code> parameter?</p> <br /><h3>回答1:</h3><br /><p>curl is an alias for PowerShell - Invoke-WebRequest, hence the error.</p> <pre><code># Get parameters, example, full and Online help for a cmdlet or function (Get-Command -Name Invoke-WebRequest).Parameters Get-help -Name Invoke-WebRequest -Examples Get-help -Name Invoke-WebRequest -Full Get-help -Name Invoke-WebRequest -Online Get-Alias -Definition Invoke-WebRequest | Format-Table -AutoSize -Wrap CommandType Name Version Source ----------- ---- ------- ------ Alias curl -&gt; Invoke-WebRequest Alias iwr -&gt; Invoke-WebRequest Alias wget -&gt; Invoke-WebRequest </code></pre> <p>If you are trying to use real curl in PowerShell, then you must use curl.exe, or remove the curl alias from Invoke-WebRequest.</p> <p>The errors are because passing parameters/arguments that Invoke-WebRequest has no idea what they are or what to do with the.</p> <p>If you are trying to use external tools in PowerShell, then you have to fully qualify the UNC and name including the externtion, to them and remember that using external tools with PowerShell, this must be approached in a defined way.</p> <p>For example:</p> <p>See Using Windows PowerShell to run old command line tools (and their weirdest parameters) 'https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters'</p> <p>See also this post regarding trying to use real curl with PowerShell.</p> <blockquote> <p>How to use the curl command in PowerShell?</p> <p>Am using the curl command in PowerShell to post the comment in bit-bucket pull request page through a Jenkins job. I used the below PowerShell command to execute the curl command, but am getting the error mentioned below. Could anyone please help me on this to get it worked?</p> <p>How to use the curl command in PowerShell?</p> </blockquote> <br /><br /><br /><h3>回答2:</h3><br /><p>I managed to do what I needed with</p> <pre><code>$url = "https://api.box.com/2.0/users/111111111" $headers = @{"Authorization" = "Bearer TOKEN"} $body = '{"status": "inactive"}' $contentType = "application/json" Invoke-WebRequest $url -Headers $headers -ContentType $contentType -Body $body -Method PUT </code></pre> <p>So it seems that not only did I need to replace the <code>-D</code> parameter with <code>-Body</code>, but I also had to specify the <code>ConteType</code> as <code>application/json</code> in order to use that format.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/48495004/how-to-send-curl-update-request-with-d-parameter</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/powershell" hreflang="zh-hans">powershell</a></div> <div class="field--item"><a href="/tag/curl" hreflang="zh-hans">curl</a></div> <div class="field--item"><a href="/tag/box-api" hreflang="zh-hans">box-api</a></div> </div> </div> Sun, 05 Jan 2020 22:00:27 +0000 不问归期 3123960 at https://www.e-learn.cn