Office 365 Email Watcher - c#

I wanted to write a class library to watch over an inbox for new mails with a particular subject and download the attachment. The end goal is to publish the library in Azure as a WebJob / API with the ability to use this library to host it on-premise. We are also planning to integrate this with a workflow functionality that we have already developed. I have found the following stuff online but have some limitations with each.
Logic Apps has a connector but I will have to write something separate for on-premise.
Office Rest APIS has several APIs exposed but all of these has redirect URL for logging user in. I wanted something like a service principal in this case which can call the URL directly without redirection to MS login page.
Other articles uses Interop DLLs which I guess would create problems when hosting as a WebJob as I have seen in the past.
Please help me with the what would be the best approach and if there is a library built in for this which I'm not able to find.
Thanks a lot in advance.

The Microsoft Graph API would be a pretty good place for getting user emails.
Here is the operation you are looking for: https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/user_list_messages
So you will need to make a request to something like:
https://graph.microsoft.com/v1.0/users/user#company.com/messages
And you can do this with a service principal. You will need the Mail.Read app-only permission (Read mail in all mailboxes). This will require you to be an Azure AD admin to consent. You can find details on the permission scopes here: https://graph.microsoft.io/en-us/docs/authorization/permission_scopes.
Oh, and there are SDKs so you don't need to write all the stuff yourself: https://graph.microsoft.io/en-us/code-samples-and-sdks

Related

Console Application to retrieve Domain Shared Contacts (Google Apps Admin SDK)

I need to write a console application to retrieve domain shared contacts (eventually update, add or delete them, too).
What I found so far is "Google Domain Shared Contacts API" should do just that, but I am a bit clueless as where to start.
I don't know how to access this API in .NET and I haven't found any examples of such a code.
As far as I can see in the documentation, Domain Shared Contacts API is not currently included in the list of Supported Google APIs for .NET .
You may, however, check in Release Notes for the Google API Client Library for .NET for announcement or updates regarding this API.
If the time comes that Domain Shared Contacts API becomes supported, you may use the following references to get started:
Easily access Google APIs from .NET
GitHub post - google-api-dotnet-client
GitHub post - google-api-dotnet-client-samples
I managed to come up with an application that sends http requests to the API, however their documentation is terrible as it lacks complete description of how exactly the requests should look like and I had to experiment a bit.

Creating C# Authentication using Forum DB at remote website?

I cant seem to find the relevant resource for learning this. I am trying to achieve a simple login for my C# application where users will be required to authenticate to use the application, and such the users account level would determine what can be accessed inside the app.
Lets say i have a forum and i want my users to authenticate via the application using the forum credentials, is this possible? what should i know about Security and how is this accomplished?
My thoughts would be i should be able to authenticate using some kind of SQL library? though i am new to C# so i might not be on the right track here.
Thanks for your suggestions.
To authenticate with a web site (forum) you need something like OAuth, see this guide for example. The website needs to provide OAuth.
Authenticating with a web site that was not explicitly designed to support an app client is not really possible, at least not correctly.

Can I move a Google API implementation to a web-service?

I have an in-house CMS system written in ASP.NET C#, and I need to implement a few pages that can allow the user to access and alter their Google Analytics management settings using the Google Analytics management API.
I have added the Google.Apis.Analytics.v3 library from NuGet, and I managed to make it work using OAuth2 authentication.
However, the library installs several dependencies, and some of them are upgrading some of my existing DLL files. This is not good, because I have a lot of functionality in this solution and some other solutions that are based on these old DLLs.
Would it be possible to move all the Google API implementation into an external web-service and just call this from my CMS? That way, I would not have to upgrade the CMS's old references. The only problem that I've run into with this is that I have no idea how to use OAuth2 to allow the CMS user to use his own Google account since all this is now done in the web-service, and the authentication page from google will not open in the user's browser.
I don't think a web service will work. The user needs to be prompted for authentication with the web browser. You cant really do that from a web service and return the proper authentication.
The Google .net client lib uses the newest DLLs you may want to look into upgrading yours.

GData Authentication for Server Apps

I have an application running on a server that periodically extracts data from Google Analytics. It doesn't issue many queries so I don't think any limit would be a problem.
I have a prototype running, however I had to provide a login a password for the authentication (in code).
I see that I can generate API keys and other kind of authentication bits and pieces in the Google APIs Console, however I am not sure how to use them and whether they can be used in place of the login/password.
It just doesn't seem right to use a user login/password to authenticate a server application.
Is there another way?
(I am using the C# client library)
You should be using OAuth 2.0. See:
http://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/oauth2_sample/oauth2demo.cs
for a sample OAuth workflow.
The best way to do this is with Google Service Accounts
https://developers.google.com/accounts/docs/OAuth2ServiceAccount
However, I don't know off the top of my head the best library for c#, but there does appear to be support for it in the Google library. http://code.google.com/p/google-api-dotnet-client/wiki/OAuth2#Service_Accounts
Here's a answer specifically about the PHP client.
Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?

How to write a command line C# program that posts to Twitter

I'd like to write a console program in C# that posts a Tweet to Twitter. I've never used the Twitter APIs before and don't know anything about how their authentication works. I found an API library called Twitterizer, but it seems geared towards web applications and wants the user to logon with a web browser. All the API docs on Twitter's website seems geared around this scenario as well.
Is it possible to access the Twitter APIs using a console app with no web browser access? I'm perfectly fine hard coding in the name and password for the Twitter user I want to post under as well. Thanks!
Mike
You'll need to use OAuth for authenticating in twitter.
Then use regular HTTP Request to use the twitter JSON-based API.
Here you can find a good article about OAuth, Twitter and console applications.
Also take a loot at linq2twitter lib. From it's documentation;
The Twitter API is built using
Representable State Transfer (REST).
Wikipaedia defines REST as "...a style
of software architecture for
distributed hypermedia systems...",
but I'm going to be so bold as to try
to simplify what that means. In
practice, REST is a Web service
protocol built upon Hypertext Transfer
Protocol (HTTP). You use the REST Web
service by making an HTTP call with a
URL and getting text back in some
form, which is often XML or JSON. So,
if you were to write code that made an
HTTP request with the following URL:
http://api.twitter.com/1/statuses/public_timeline.xml
You would get back an XML document
with all of the Twitter statuses from
the public timeline, which is a
snapshot in time of the last 20 tweets
at the time of your request. Go ahead
and open your browser, copy and paste
the URL above into the address bar,
and see what you get back.
I couldn't find any decent information on the web on how to do this, so I decided to write my own blog post with all the details.. Enjoy!
http://blog.kitchenpc.com/2011/01/22/rise-of-the-twitterbot/
of course you can use anything to connect to Twitter via RESTful api.
you should use oauth, and set up your application in http://dev.twitter.com, then you should read all articles listed in documents, you must specify your app as Client but not Browser so user input a number to get through authentication.
you can use many libraries so that you can save your time, all are listed in the documents
and be CAREFUL, you should not use Twitter's own api console which is buggy (as i know parameters somtimes can't be parsed), you should use APIgee instead which is powerful and stable.
if you want use basic authentication, you should use api proxy (one famous is twip), if you just need only one single C# apps, you must code by yourself:
you should use given username and password to login twitter, parse cookies passed
use normal oauth to get temporaly access token url.
use cookies got from step 1, emulates form submit to allow your apps, capture PIN code
use pin code to finish oauth.
MOST IMPORTANT, you must store access token in client's machine so next time you can bypass above steps
Just wrote a Twitter Bot in C#. This is currently posting tweets to #valuetraderteam.
https://gist.github.com/sdesalas/c82b92200816ecc83af1
The API component in the GIST below is less than 500 lines, only dependency is Json.NET, you'll need to download the latest DLL for either x64 or x86 (depending on what platform you are targetting) and include as a reference in your project.
There is an example at the bottom of the page of how you can make a tweet from a console application
Hopefully this is useful to some other people out there.

Categories