jhoerr / box-csharp-sdk-v2

A C# client for the Box API (v2).
http://developers.box.com/docs/
11 stars 15 forks source link

GetFolder() method not getting all the fields. #56

Closed anushrikinhikar closed 10 years ago

anushrikinhikar commented 10 years ago

Hi jhoerr,

I am calling the GetFolder() method like below:

boxManager.GetFolder(Folder.Root, new[] { FolderField.Etag, FolderField.Name });

but here it is giving me "Object reference not set to instance of an object" error. When i debugged it, I found the error is coming in: line while (folder.ItemCollection.Entries.Count < folder.ItemCollection.TotalCount) in the below function since i m getting folder.ItemCollection = null

private Folder DoGetFolderSync(string id, IEnumerable fields, string etag, int? limit, int? offset, Func<BoxRestClient, BoxRestClient> prepareClient) { GuardFromNull(id, "id"); var paginate = limit.HasValue || offset.HasValue; var request = _requestHelper.Get(ResourceType.Folder, id, fields, etag, paginate ? limit : MaxItems, paginate ? offset : 0); var folder = prepareClient(_restClient).ExecuteAndDeserialize(request);

        if (!paginate)
        {
            offset = MaxItems;
            while (folder.ItemCollection.Entries.Count < folder.ItemCollection.TotalCount)
            {
                request = _requestHelper.Get(ResourceType.Folder, id, fields, etag, MaxItems, offset);
                var next = prepareClient(_restClient).ExecuteAndDeserialize<Folder>(request);
                folder.ItemCollection.Entries.AddRange(next.ItemCollection.Entries);
                offset += MaxItems;
            }
        }
        return folder;
    }

Please let me know what is the correct code to fetch all the details of the folder/file

jhoerr commented 10 years ago

Your code should work. Please post a Fiddler trace of the HTTP request and response that's generating this error.

anushrikinhikar commented 10 years ago

Hi John,

What I want is to access Box files under my account using .net and display it in a web application.

I am using the V1 API method to create an object of BoxManager class.

BoxManager boxManager = new BoxManager("MyAPIKey", "MyAuthToken");

Is it because of this that the function GetFolder is failing ?

It would be great if you could help me with the V2 BoxManager class. I tried creating an instance by putting in the values "accesstoken", "clientId", "clientSecret" and "refreshToken" as below:

var authenticator = new TokenProvider(clientId, clientSecret); var oAuthToken = authenticator.RefreshAccessToken(refreshToken); accessToken = oAuthToken.AccessToken;

// Instantiate a BoxManager with your api key and a user's auth token var boxManager = new BoxManager(accessToken);

This works fine but what should be the step if the access token expires and the refresh token expires.

Could you provide me some code for refreshing the access token from the code since I dont want the user to experience any session timeout or something like that.

Appreciate you response !!!

Thanks, Anushri Kinhikar

On Tue, Jan 7, 2014 at 8:15 PM, John Hoerr notifications@github.com wrote:

Your code should work. Please post a Fiddler trace of the HTTP request and response that's generating this error.

— Reply to this email directly or view it on GitHubhttps://github.com/jhoerr/box-csharp-sdk-v2/issues/56#issuecomment-31742598 .

anushrikinhikar commented 10 years ago

Hi John,

I dont have fiddler trace just the stack trace if this could help.

[BoxException] BoxApi.V2.BoxRestClientBase.Try(IRestRequest request, HttpStatusCode lastResponse) +913 BoxApi.V2.BoxRestClientBase.Execute(IRestRequest request) +66 RestSharp.RestClient.Execute(IRestRequest request) +35 BoxApi.V2.BoxRestClientBase.ExecuteAndDeserialize(IRestRequest request) +140 BoxApi.V2.BoxManager.DoGetFolderSync(String id, IEnumerable1 fields, String etag, Nullable1 limit, Nullable1 offset, Func2 prepareClient) +590 BoxApi.V2.BoxManager.GetFolder(String id, IEnumerable1 fields, String etag, Nullable1 limit, Nullable`1 offset) +326 WebApplication2._Default.Page_Load(Object sender, EventArgs e) in C:\TestApp\BoxNetAPI\WebApplication2\WebApplication2\Default.aspx.cs:28 System.Web.UI.Control.LoadRecursive() +71 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

I needed this on an urgent basis.

Please let me know if you have any clue about this.

Thanks,

Anushri Kinhikar

On Tue, Jan 7, 2014 at 8:15 PM, John Hoerr notifications@github.com wrote:

Your code should work. Please post a Fiddler trace of the HTTP request and response that's generating this error.

— Reply to this email directly or view it on GitHubhttps://github.com/jhoerr/box-csharp-sdk-v2/issues/56#issuecomment-31742598 .

jhoerr commented 10 years ago

Hi Anushri,

The V1 API has been deprecated by Box; you'll need to use the V2 version.

...what should be the step if the access token expires and the refresh token expires.

If the access token expires, simply call authenticator.RefreshAccessToken(refreshToken) to get a new access/refresh token pair. You then need to create a new instance of the BoxManager with the new access token.

The refresh token expires after 14 days. If it's allowed to expire the user must reauthenticate your application. You can work around this limitation by periodically refreshing access/refresh tokens that have been idle for several days.

Could you provide me some code for refreshing the access token

You're already aware of and using the code to accomplish this task. The only recommendation I could make is to wrap your operations in a try/catch clause that looks for HTTP 401 errors. If a 401 is encountered, that will likely indicate that the access token is expired and needs to be refreshed. You will then need to refresh the token and retry the operation.

anushrikinhikar commented 10 years ago

Thanks John,

Any updates on the GetFolder() method.

Regards, Anushri Kinhikar

On Thu, Jan 9, 2014 at 8:20 PM, John Hoerr notifications@github.com wrote:

Hi Anushri,

The V1 API has been deprecated by Box; you'll need to use the V2 version.

...what should be the step if the access token expires and the refresh token expires.

If the access token expires, simply call authenticator.RefreshAccessToken(refreshToken) to get a new access/refresh token pair. You then need to create a new instance of the BoxManager with the new access token.

The refresh token expires after 14 days. If it's allowed to expire the user must reauthenticate your application. You can work around this limitation by periodically refreshing access/refresh tokens that have been idle for several days.

Could you provide me some code for refreshing the access token

You're already aware of and using the code to accomplish this task. The only recommendation I could make is to wrap your operations in a try/catch clause that looks for HTTP 401 errors. If a 401 is encountered, that will likely indicate that the access token is expired and needs to be refreshed. You will then need to refresh the token and retry the operation.

— Reply to this email directly or view it on GitHubhttps://github.com/jhoerr/box-csharp-sdk-v2/issues/56#issuecomment-31937961 .

anushrikinhikar commented 10 years ago

Hey John,

I need to use the refresh token as there in the screenshot right ? This is the refresh token that expires 14 days ? Because this refresh token is expiring for me after some minutes itself.

[image: Inline image 1]

On Fri, Jan 10, 2014 at 9:02 AM, Anushri Kinhikar < anushri.kinhikar@gmail.com> wrote:

Thanks John,

Any updates on the GetFolder() method.

Regards, Anushri Kinhikar

On Thu, Jan 9, 2014 at 8:20 PM, John Hoerr notifications@github.comwrote:

Hi Anushri,

The V1 API has been deprecated by Box; you'll need to use the V2 version.

...what should be the step if the access token expires and the refresh token expires.

If the access token expires, simply call authenticator.RefreshAccessToken(refreshToken) to get a new access/refresh token pair. You then need to create a new instance of the BoxManager with the new access token.

The refresh token expires after 14 days. If it's allowed to expire the user must reauthenticate your application. You can work around this limitation by periodically refreshing access/refresh tokens that have been idle for several days.

Could you provide me some code for refreshing the access token

You're already aware of and using the code to accomplish this task. The only recommendation I could make is to wrap your operations in a try/catch clause that looks for HTTP 401 errors. If a 401 is encountered, that will likely indicate that the access token is expired and needs to be refreshed. You will then need to refresh the token and retry the operation.

— Reply to this email directly or view it on GitHubhttps://github.com/jhoerr/box-csharp-sdk-v2/issues/56#issuecomment-31937961 .

jhoerr commented 10 years ago

Any updates on the GetFolder() method.

As far as I know that method is working properly. I can't help you any more until you post a Fiddler trace of the HTTP request and response that's generating the error.

Because this refresh token is expiring for me after some minutes itself.

I can't see the image you posted. But anyway, I don't think the refresh token is expiring after a few minutes -- that would effectively kill Box's entire API for all users. Are you aware that refresh token is single use? That is, when you use the refresh token, a new refresh token is issued along with the new access token?

anushrikinhikar commented 10 years ago

Hi John,

I will share the client_id and client_secret of my api application with you. Is it possible for you to just send me a working code snippet with the connection to BoxManager class using my credentials and getting all folder details in it.

Could you use my credentials and other keys to find this oauth2AccessToken and the refresh token (which expires after 14 days) since the refresh token that I am getting is valid only for some time. If I try to use the refresh token after some time, it gives me error saying "invalid refresh_token"

clientid="****" clientsecret="**" username="anushri.kinhikar@gmail.com pwd="**" API application name= "ABCDEFGH"

Please try this and let me know. I need this to be done asap.

Thanks, Anushri Kinhikar

On Fri, Jan 10, 2014 at 8:45 PM, John Hoerr notifications@github.comwrote:

Any updates on the GetFolder() method.

As far as I know that method is working properly. I can't help you any more until you post a Fiddler trace of the HTTP request and response that's generating the error.

Because this refresh token is expiring for me after some minutes itself.

I don't think that's the case -- it would effectively kill Box's entire API for all users. Are you aware that refresh token is single use? That is, when you use the refresh token, a new refresh token is issued along with the new access token?

— Reply to this email directly or view it on GitHubhttps://github.com/jhoerr/box-csharp-sdk-v2/issues/56#issuecomment-32035158 .

jhoerr commented 10 years ago

Anushri, I'm sorry, I can't do this for you right now. Have you looked at the usage example? It contains working code.

If this SDK isn't meeting your needs, you might check out Box's official Windows SDK.

anushrikinhikar commented 10 years ago

Hi John,

I took exactly the same example code. Ok, how do I find out the access token and refresh token ? Is there any way of finding the refresh token for my api. I used the link of Box Token Generator: https://box-token-generator.herokuapp.com/ Here when i enter the client id and client secret, it gives me access token and refresh token. I use this access token for boxmanager class object. But after some time this access token expires. So, for refreshing this access token, I use the refresh token which i got prior, then it gives me invalid refresh_token error.

Is there anything that I may be missing on. I am clueless and I need this to do as soon as possible.

Thanks, Anushri Kinhikar

On Wed, Jan 15, 2014 at 6:26 PM, John Hoerr notifications@github.comwrote:

Anushri, I'm sorry, I can't do this for you right now. Have you looked at the usage examplehttps://github.com/jhoerr/box-csharp-sdk-v2#usage-example? It contains working code.

If this SDK isn't meeting your needs, you might check out Box's official Windows SDK https://github.com/box/box-windows-sdk-v2.

— Reply to this email directly or view it on GitHubhttps://github.com/jhoerr/box-csharp-sdk-v2/issues/56#issuecomment-32358952 .