ringcentral / ringcentral-csharp-client

RingCentral C# Client
https://developer.ringcentral.com
19 stars 19 forks source link

How do I make an API call just to validate account info? #4

Closed jmol123 closed 7 years ago

jmol123 commented 7 years ago

I tried looking through the RingCentral.Test folder but I can't figure out how to write the code that simply makes a request to authenticate the API? Is there documentation on this somewhere that I can follow for the C# client, or perhaps some sample code? How would I pull the Test into my environment and change the credentials to the one for my app?

Thanks!!

tylerlong commented 7 years ago

In Test project, code for authorization is here: https://github.com/ringcentral/ringcentral-csharp-client/blob/master/RingCentral.Test/RestClientFixture.cs#L20

How to setup the test project: https://github.com/ringcentral/ringcentral-csharp-client/blob/master/RingCentral.Test/README.md

If your question is how to do authorization, here is the sample code: https://github.com/ringcentral/ringcentral-csharp-client#authorization

jmol123 commented 7 years ago

Where do I run that line of code in the Readme to get that into my project?

Thank you for your speedy response!!

On Thu, Jan 19, 2017 at 8:50 PM Tyler Long notifications@github.com wrote:

In Test project, code for authorization is here: https://github.com/ringcentral/ringcentral-csharp-client/blob/master/RingCentral.Test/RestClientFixture.cs#L20

How to setup the test project: https://github.com/ringcentral/ringcentral-csharp-client/blob/master/RingCentral.Test/README.md

If your question is how to do authorization, here is the sample code: https://github.com/ringcentral/ringcentral-csharp-client#authorization

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ringcentral/ringcentral-csharp-client/issues/4#issuecomment-273954723, or mute the thread https://github.com/notifications/unsubscribe-auth/AXpkfUj9WGsYI1dHs9jbkAc0Z-oZjgZ6ks5rUBLsgaJpZM4LozO4 .

tylerlong commented 7 years ago

In this folder where you can find config.sample.json: https://github.com/ringcentral/ringcentral-csharp-client/tree/master/RingCentral.Test

jmol123 commented 7 years ago

Okay so once I install the Client using NuGet do I have to pull the GitHub code here? Thanks, and sorry if these questions are silly! It's my first time doing this!! Thanks!

On Thu, Jan 19, 2017 at 9:36 PM Tyler Long notifications@github.com wrote:

In this folder where you can find config.sample.json: https://github.com/ringcentral/ringcentral-csharp-client/tree/master/RingCentral.Test

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ringcentral/ringcentral-csharp-client/issues/4#issuecomment-273961695, or mute the thread https://github.com/notifications/unsubscribe-auth/AXpkfYrN84s8zYxawEgc9oiYF3OdfSKvks5rUB2ygaJpZM4LozO4 .

tylerlong commented 7 years ago

I need to know why do you want to do this. Because code here https://github.com/ringcentral/ringcentral-csharp-client/tree/master/RingCentral.Test is for developers of this library to test this library.

As an end user, you are not required to test this library. You can however report issues if you've found any.

If you want to use this library. Just create your own project(based on your requirements) and use Nuget to add this library to your own project. There is no need to download source code.

Feel free to post more questions.

jmol123 commented 7 years ago

I am currently trying to use RingCentral Client to develop an API that will download faxes that come in to a specific extension directly to my computer.

tylerlong commented 7 years ago

It should be trivial to do. You don't need to download the source code of this project. What you need to do is to create a project, use nuget to add this library to your own project and start to write some code.

Some sample code for your reference:

var rc = new RestClient("appKey", "appSecret", true); // true is for production and false is for sandbox
var temp = rc.authorize("username", "extension", "password").Result;
var extension = rc.Restapi().Account().Extension("extensionId or ~ for the current extension");
var response = extension.MessageStore().List().Result;
var messages = response.records;
messages = messages.Where(m => m.type == "Fax" && m.attachments != null && m.attachments.Length > 0);
foreach(var message in messages)
{
     content = extension.MessageStore(message.id).Content(message.attachments[0].id).Get().Result;
     File.WriteAllBytes("fax.pdf", content.data); // save fax as pdf
}

Please note that, code above was written by me in 5 minutes and I didn't even try to compile it. So it is just for your reference, you might need to change it in order to make it fully working.

We do have some working code for fax downloading here: https://github.com/ringcentral/ringcentral-csharp-client/blob/master/RingCentral.Test/BinaryTest.cs#L62

Please leave messages if you have questions.

jmol123 commented 7 years ago

Hi Tyler,

I tried using the code you have written above, and it appears to not make any API call request, as my dashboard shows no requests. Should I do something with the temp variable that would allow it to make a call to the API?

Thank you very much for all of your help!!

tylerlong commented 7 years ago

You are welcome.

The List() and Get() methods are requests.

You can zip and send your sample project to me: tyler.liu@ringcentral.com (remove your credentials)

I will take a look as soon as I receive it.

jmol123 commented 7 years ago

Hi Tyler,

I just wanted to let you know that I sent an email to the address you provided.

Thank you very much again, and I'm very grateful for all the help you've been giving me!!

On Mon, Jan 23, 2017 at 8:43 PM Tyler Long notifications@github.com wrote:

You are welcome.

The List() and Get() methods are requests.

You can zip and send your sample project to me: tyler.liu@ringcentral.com (remove your credentials)

I will take a look as soon as I receive it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ringcentral/ringcentral-csharp-client/issues/4#issuecomment-274676149, or mute the thread https://github.com/notifications/unsubscribe-auth/AXpkfb9_ZEDZcMlGCXLtgEMwum2jWfyJks5rVVdOgaJpZM4LozO4 .

tylerlong commented 7 years ago

You are welcome!

Sample code:

using RingCentral;
using System;
using System.IO;

namespace FaxDownloadingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var outputFolder = @"C:\Users\tyler\Desktop";

            // for sandbox environment
            var rc = new RestClient("appKey", "appSecret", false);
            // for production environment:
            // var rc = new RestClient("appKey", "appSecret", true);
            var temp = rc.Authorize("username", "", "password").Result;
            Console.WriteLine("logged in");

            var extension = rc.Restapi().Account().Extension();
            var faxes = extension.MessageStore().List(new { messageType = "Fax", perPage = 1000 }).Result.records;
            Console.WriteLine(String.Format("{0} faxes were found on the server", faxes.Length));

            var faxId = 0;
            foreach (var fax in faxes)
            {
                faxId += 1;
                if (fax.attachments != null && fax.attachments.Length > 0)
                {
                    var binary = extension.MessageStore(fax.id).Content(fax.attachments[0].id).Get().Result;
                    File.WriteAllBytes(string.Format(@"{0}\fax-{1}.pdf", outputFolder, faxId), binary.data);
                    Console.WriteLine("Download fax #{0}", faxId);
                }
            }

            Console.WriteLine("Done. Press enter to exit");
            Console.ReadLine();
        }
    }
}

Sample output:

image

Output folder:

image

I've also mailed the working project to you.

Any more questions, just let me know.

jmol123 commented 7 years ago

Hi Tyler,

I used your sample code exactly with my credentials and I got the output saying no faxes were found on the server, but there are 2 faxes in that extension's message store I believe: image

Message Store of extension: image

Interestingly, no API calls were made to me API according to the dashboard. image

Do you have any idea what could be causing this?

Thanks again!!

jmol123 commented 7 years ago

Update: The API calls showed up in the dashboard so it appears to be making contact with the API, but it's strange that it isn't finding any faxes. Do they have to be new faxes? Thanks again!!

jmol123 commented 7 years ago

Update 2.0: Hey Tyler, I just sent a fax a few minutes ago to the extension, and when I got the notification saying it sent, I ran the code and it was able to download the fax!! YAY! My next question is: How far back does this code look for faxes? Is it within the last 7 days or what? Thanks!!

Also, I am going to begin getting a subscription set up for this so I am able to download it as soon as the fax comes in automatically. Would you happen to have in the RingCentral.Test project some sample code on this?

jmol123 commented 7 years ago

Sorry for the amount of posts since the last time you posted, but I just thought of another question I have. Is there a list of info that I can get from the message store in addition to just the attachment, such as who sent the fax and what time it came in? What other information comes along with the attachment/message itself that I can pull from to make a more descriptive filename?

Thanks again again again!!

tylerlong commented 7 years ago

Yes. you are right. By default it only download fax for the last 24 hours.

For you questions, please read the documentaion: https://developer.ringcentral.com/api-docs/latest/index.html#!#RefMessageList.html

Some screenshots for your quick reference:

image

You are also specify "Inbound" or "Outbound" as query parameter.

Is there a list of info that I can get from the message store in addition to just the attachment, such as who sent the fax and what time it came in? What other information comes along with the attachment/message itself that I can pull from to make a more descriptive filename?

Yes, all the information you requested is in the documentation: https://developer.ringcentral.com/api-docs/latest/index.html#!#MessageInfo

A screenshot for your quick reference:

image

You should be able to find everything from the documentation.

Again you are welcome! You don't need to thank me because it's my job to support the developers. Just redirect you question to me and absolutely no hard feelings.

Feel free to make this thread even longer!

tylerlong commented 7 years ago

For realtime message monitoring(fax is a special kind of message), here is some sample code: https://github.com/ringcentral/ringcentral-csharp-client#subscription

For your requirement, please try the following:

var subscription = rc.Restapi().Subscription().New();
subscription.EventFilters.Add("/restapi/v1.0/account/~/extension/~/message-store");
subscription.NotificationEvent += (sender, args) => {
    var notification = args.notification;
    Console.WriteLine(notification.json); // here is the json from server.
    // you can work with the json directly if you want, then switch statement below is unnecessary
    // or you can use the code below to handle the json.
    switch (notification.type)
    {
        case NotificationType.Message:
            var messageNotification = notification.Downcast<MessageNotification>();
            // do something with messageNotification
            break;
        default:
            break;
    }
};
subscription.Register().Result;
...
// You might need to pause your application here otherwise it will exit.

For message structure of notification, please read https://developer.ringcentral.com/api-docs/latest/index.html#!#RefGetMessageEvent

If you have problems get it up and running. please zip and send me your sample code.

jmol123 commented 7 years ago

Thank you very much for all that useful info Tyler!!

I have a question regarding the realtime monitoring. I can insert code in the monitor to have it download the fax on its own correct?

Thanks again!!

On Tue, Jan 24, 2017 at 8:14 PM, Tyler Long notifications@github.com wrote:

For realtime message monitoring, here is some sample code: https://github.com/ringcentral/ringcentral-csharp-client#subscription

For your requirement, please try the following:

var subscription = rc.Restapi().Subscription().New(); subscription.EventFilters.Add("/restapi/v1.0/account/~/extension/~/message-store"); subscription.NotificationEvent += (sender, args) => { var notification = args.notification; Console.WriteLine(notification.json); // here is the json from server. // you can work with the json directly if you want, then code below is unnecessary // or you can use the code below to handle the json. switch (notification.type) { case NotificationType.Message: var messageNotification = notification.Downcast(); // do something with messageNotification break; default: break; } }; subscription.Register().Result; ...// You might need to pause your application here otherwise it will exit.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ringcentral/ringcentral-csharp-client/issues/4#issuecomment-274991203, or mute the thread https://github.com/notifications/unsubscribe-auth/AXpkfdS8vRq_hC0e_kzghU3ssO6GwbRcks5rVqH7gaJpZM4LozO4 .

-- Julian Molina-Goni

IT Specialist

InformedDNA

Office: 760.359.7060

Fax: 800.930.0961

http://informeddna.com/

  <https://www.facebook.com/Informeddnageneticexperts>

http://www.informeddna.com/

tylerlong commented 7 years ago

I can insert code in the monitor to have it download the fax on its own correct?

Yes, but not very direct. Because the realtime monitoring notification is something like the following:

{"uuid":"5459128613393766018-700640767071453334","event":"/restapi/v1.0/account/130829004/extension/130829004/message-store","timestamp":"2
017-01-25T02:48:00.448Z","subscriptionId":"0f688bcb-e084-4d2a-8aac-67bcb95d9f7b","body":{"extensionId":130829004,"lastUpdated":"2017-01-25T
02:47:46.412+0000","changes":[{"type":"Fax","newCount":0,"updatedCount":1}]}}

There is no ID for the fax, so you can NOT just run the following:

var fax = extension.messageStore(id).Get().Result; // doesn't work because you don't have the `id`
var binary = extension.MessageStore(fax.id).Content(fax.attachments[0].id).Get().Result;

But you can always fetch the fax list and download the latest ones once you get the notification (a.k.a. the code we wrote above).

jmol123 commented 7 years ago

Hi Tyler,

Would you happen to know of a way to pull just the last 10 minutes worth of faxes based on the method that doesn't involve realtime monitoring? Thanks!!

tylerlong commented 7 years ago

Here it goes:

var dateFrom = DateTime.UtcNow.AddMinutes(-10).ToString("o"); // 10 minutes ago
var faxes = extension.MessageStore().List(new { messageType = "Fax", perPage = 1000, dateFrom = dateFrom }).Result.records;
jmol123 commented 7 years ago

Thanks Tyler! That worked great!!

I'm not sure if this is the right place to ask but do you know if there is a limit to the number of API calls a day per endpoint or extension or anything? Also, how often does a token have to be renewed?

Thanks!!

jmol123 commented 7 years ago

Hi Tyler, When I try to customize the filename using some of the data from the faxes I get an error saying the path is not valid. Do you see what could cause that? Thanks again!!!

>  var faxId = 0;
>             foreach (var fax in faxes)
>             {
>                 faxId += 1;
>                 if (fax.attachments != null && fax.attachments.Length > 0)
>                 {
>                     var binary = extension.MessageStore(fax.id).Content(fax.attachments[0].id).Get().Result;
>                     File.WriteAllBytes(string.Format(@"{0}\{1}-{2}-faxid-{3}.pdf", outputFolder, fax.creationTime.ToString(), fax.from.ToString(), faxId), binary.data);
>                     Console.WriteLine("Download fax #{0}", faxId);
>                     Console.WriteLine(fax.subject);
>                 }
>             }
> 
jmol123 commented 7 years ago

The project now gets 400 errors with the code I have above, and I believe there is an unhandled exception, but I just don't know where it is. Thanks!!

tylerlong commented 7 years ago

I'm not sure if this is the right place to ask but do you know if there is a limit to the number of API calls a day per endpoint or extension or anything?

API Rate Limites: http://ringcentral-api-docs.readthedocs.io/en/latest/rate_limits/

Within the above presented limits your client application is allowed to send 10 heavy, 40 medium, 50 light and 5 authorization requests per user (RC extension) per minute.


Also, how often does a token have to be renewed?

Token refresh is handled automatically for you by this library unless you disable it explicitly. Renew your token doesn't reclaim the rate limit quota. If you want to do it manually, you can invoke rc.Refresh();


When I try to customize the filename using some of the data from the faxes I get an error saying the path is not valid.

It seems like a C# issue. The path you specified is not valid. We know that some special charaters are not allowed in the path. You can Console.WriteLine to see what special characters are there.


The project now gets 400 errors with the code I have above, and I believe there is an unhandled exception, but I just don't know where it is.

400 error probably means either of the following:

For either case, it's helpful to catch the exception and print the error message from server:

catch (FlurlHttpException ex) {
    dynamic d = ex.GetResponseJson();
    string s = ex.GetResponseString();
}
jmol123 commented 7 years ago

Thank you for all of that useful info! Do you know how to pull the phone # of the sender of the fax? That is the last piece of info I need and I don't know how to pull it. Thanks again!!

tylerlong commented 7 years ago

Frankly speaking I don't have direct experience on your last question. I am currently on vacation and unable to test it myself.

If the documentation is correct there should be a filed named from: https://developer.ringcentral.com/api-docs/latest/index.html#!#MessageInfo

If you cannot get it working, could you please post your question to https://devcommunity.ringcentral.com/ringcentraldev? I ask you to do this because your question is not actually about C# and I am currently unable to help you( I am on vacation)

It will help others to troubleshoot the issue quickly if you can provide the data you are already able to fetch. (Post sample of the data you get from the server and specify what data is missing.)

jmol123 commented 7 years ago

Hey Tyler!! I hope you enjoy your vacation! I was able to figure that out on my own, but I have another question for you.

Say I wanted to check another extension in addition to the main "101" one. Would I have to reauthorize, or how would I do that? Thanks again!!

jmol123 commented 7 years ago

And another question! Am I annoying enough yet? I'm really sorry and I thank you so much for all the help you've given me Tyler!!

Would you happen to have some sample code on how to pull VoiceMails so that they go into a folder on my server as .mpeg files or something like that (similar to how the faxes are working, but with VMs)? I am looking for a way to download the voice mails as well as the faxes.

Thanks again!!

tylerlong commented 7 years ago

Say I wanted to check another extension in addition to the main "101" one. Would I have to reauthorize, or how would I do that? Thanks again!!

No, you don't have to, as long as the current user you authorized has the permission to access information of other extensions. For example, an admin user account.

Sample code:

// the same as before
// ... 
var extension = rc.Restapi().Account().Extension("extension ID");
// ...
// the same as before

Please note that "extension ID" is not something like "101", "102"....etc. It's the internal extension ID of the system. You can look up all the extensions by rc.Restapi().Account().Extension().List()

tylerlong commented 7 years ago

sample code on how to pull VoiceMails

I didn't try it but it should be similar to how you download fax. Just replace messageType = "Fax" with messageType = "VoiceMail". And when you save the content, save it as *.wav instead of *.pdf.

If you cannot get it work please post your sample code so I can provide further assistance.

Last but not least. Feel free to post more questions as long as they have something to do with RingCentral and C#!

jmol123 commented 7 years ago

Hi Tyler, I tried using .List() but I received a fault. I'm not sure what that means but I have included a screenshot of the results below. Thanks again!!

image

jmol123 commented 7 years ago

Update: The solution you proposed for the VMs work perfectly!! Thanks! Once I am able to go through the list of extensions and check the ones I need to I am ready for Production!! :)

tylerlong commented 7 years ago

Here is some sample code to go through the extensions:

var extensions = rc.Restapi().Account().Extension().List().Result.records;
foreach(var item in extensions)
{
    var extension = rc.Restapi().Account().Extension(item.id);
    // do something with extension
}
jmol123 commented 7 years ago

Hi Tyler,

I received an error "An exception of type 'System.AggregateException' occurred in mscorlib.dll but was not handled in user code" in this line: var faxes = extension.MessageStore().List(new { messageType = "Fax", perPage = 1000 }).Result.records;

Any idea what the error means? Thanks!!

tylerlong commented 7 years ago

In visual studio you should be able to see the internal exception. I have no idea unless I see the internal exception.

jmol123 commented 7 years ago

Got it. I will work out some code to alert me via email if an exception gets thrown. I noticed a new issue where I am getting 400 errors when I make the Auth Token request. I'm wondering if it would be okay to send you my code the way it is now to see if the error is caused by making too many Auth Token requests? Thanks so much once again!!

image

tylerlong commented 7 years ago

I am currently on vacation and I won't be back to work until Friday. So I am afraid that I don't have a working environment to run your sample code.

If you have very specific questions I can still answer them. For any exception, please catch it and inspect it: https://github.com/ringcentral/ringcentral-csharp-client#exception-handling

You can print the status code, the request header, the query parameter, the response body...etc. So we can see clearly that what kind of API request caused the exception and why did it happen. If you provide me with such information I guess I am able to tell you the root cause and the solution.

If you cannot manage to catch and inspect the exceptions till Friday, I will be happy to test your sample code myself to do troubleshooting.

jmol123 commented 7 years ago

Hey Tyler,

I just wanted to let you know that I just applied the app for Production!! If it is possible (after Friday) I would like to zip up my project and have you look over it just to see if you see anything that could be more optimized?

Enjoy your vacation, and thank you again for all of your help!!

On Tue, Jan 31, 2017 at 5:26 AM, Tyler Long notifications@github.com wrote:

I am currently on vacation and I won't be back to work until Friday. So I am afraid that I don't have a working environment to run your sample code.

If you have very specific questions I can still answer them. For any exception, please catch it and inspect it: https://github.com/ ringcentral/ringcentral-csharp-client#exception-handling

You can print the status code, the request header, the query parameter, the response body...etc. So we can see clearly that what kind of API request caused the exception and why did it happen. If you provide me with such information I guess I am able to tell you the root cause and the solution.

If you cannot make it fully working till Friday, I will be happy to test your sample code myself to do troubleshooting.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ringcentral/ringcentral-csharp-client/issues/4#issuecomment-276327281, or mute the thread https://github.com/notifications/unsubscribe-auth/AXpkfZ3c6wPTzOQuUnPcCKBUmKCvknz-ks5rXwxugaJpZM4LozO4 .

-- Julian Molina-Goni

IT Specialist

InformedDNA

Office: 760.359.7060

Fax: 800.930.0961

http://informeddna.com/

  <https://www.facebook.com/Informeddnageneticexperts>

http://www.informeddna.com/

tylerlong commented 7 years ago

Feel free to send your sample code to tyler.liu@ringcentral.com.

Please create another issue if you have new questions. This one is getting too long for others to comprehend. :)