Google Compute Engine Api using oAuth to list instances c# - c#

Can you please show me some c# example for Authorizing and getting instances using Google Compute engine? When I tried authorizing through some of the code that are out there it is always popping up the gmail login page. Is it necessary to use my own login username or can I use the username of the person who created the vm instances on the google cloud platform?

Can you please show me some c# example for Authorizing and getting instances using Google Compute engine?
The following code can help.
private async Task<List<Instance>> GetAllInstances()
{
List<Instance> instanceResult = new List<Instance>();
var service = assign GooglecomputeServiceObject;
InstancesResource instancesResource = new InstancesResource(service);
InstanceList instanceList = await instancesResource.List(your ProjectId, specify_Zone).ExecuteAsync();
if (instanceList.Items != null)
foreach (var item in instanceList.Items)
{
instanceResult.Add(item);
}
return instanceResult;
}

Related

How to implement SignalR to show email notifications to receivers in ASP.NET MVC 4 Application

I have a requirement to show email notification to receivers using SignalR. I am developing my application using MVC 4. I am a beginner and do not have much knowledge for SignalR. So far I have followed the posts below and tried to prepare a sample that can fulfill my requirement.
http://www.codeproject.com/Articles/732190/Real-Time-Web-Solution-for-Chat-by-MVC-SignalR-H
http://techbrij.com/realtime-post-comment-notifications-signalr-knockout
One of above is using knockout.js and other is not. I have followed both and am not able to meet my requirement.
Here is the code from SignalR Hub class:
public void SendEmail(Email email)
{
email.SentBy = WebSecurity.CurrentUserId;
using (NotifyEntities db = new NotifyEntities())
{
UsersContext uc = new UsersContext();
db.Emails.Add(email);
db.SaveChanges();
var ret = new
{
Message = post.Message,
SentBy = post.SentBy,
};
Clients.Caller.sendEmail(ret);
#region add to table which contain user id and email_post_id (I am sending the email to 2 users/receivers)
UsersMessage msgRel = new UsersMessage();
msgRel.EID = email.Id;
msgRel.UID = 2; //user 1
db.UsersMessages.Add(msgRel);
db.SaveChanges();
msgRel = new UsersMessage();
msgRel.EID = email.Id;
msgRel.UID = 5;//user 2
db.UsersMessages.Add(msgRel);
db.SaveChanges();
#endregion
var unread = (from ure in db.Emails.ToList()
join um in db.UsersMessages on ure.Id equals um.EID
where um.UID == WebSecurity.CurrentUserId && um.IsReaded == false
orderby ure.Id descending
select new
{
Message = pst.Message,
SentBy = pst.SentBy,
}).ToArray();
Clients.All.loadUnreadPosts(unread); //this returns unread emails of currently logged in user
Clients.All.loadPosts(ret); // this returns all emails of currently logged in user
}
}
When I do not use knockout.js then I am not able to show instant notification, it only appears on page refresh and it display to all users instead of the receivers.
When I use knockout.js the notification is showing instantly but the same issue exists that message/notification is displaying to all users.
Please help me with this. I don't know how to create group for receivers of particular email.
In order to send the notification to specific users only, you would want to use the Group functionality of SignalR. Here is some documentation on how to set it up: link
You would probably end up creating a group per user on user login or some similar action.
public async Task UserOnline(string emailAddress)
{
await Groups.Add(this.Context.ConnectionId, emailAddress);
}
Then you would want to use something like this in your signalr SendEmail hub class.
Clients.Group(emailAddress).loadUnreadPosts(unread);
Clients.Group(emailAddress).loadPosts(ret);
As a side note, I recommend going through the documentation at the link I provided. There should be no reason that you need to use knockout to get signalR to work appropriately.
EDIT
I have gotten something similar to the above working before (although I have lost that code); but I just came across this article that may actually provide a more elegant solution.

Windows Phone 8 - Twitter API

I am doing some research and can't seem to find a definitive way to query Twitter's API to get a user's tweets on WP8 using C#.
The twitter user isn't my own account and all I want to do is retrieve tweets and not post any tweets.
Do I therefore need to Authenticate myself/my app? Is the TweetSharp library appropriate for my objective?
I used the following example http://code.msdn.microsoft.com/wpapps/Twitter-Sample-using-f36bab75:
if (NetworkInterface.GetIsNetworkAvailable())
{
//Obtain keys by registering your app on https://dev.twitter.com/apps
var service = new TwitterService("abc", "abc");
service.AuthenticateWith("abc", "abc");
//ScreenName is the profile name of the twitter user.
service.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions() { ScreenName = "the_appfactory" }, (ts, rep) =>
{
if (rep.StatusCode == HttpStatusCode.OK)
{
//bind
this.Dispatcher.BeginInvoke(() => { tweetList.ItemsSource = ts; });
}
});
}
else
{
MessageBox.Show("Please check your internet connestion.");
}
I created a new application user the twitter dev website: https://dev.twitter.com/user/login?destination=home. Once this was set-up, I essentially copied and pasted the keys within the AuthenticateWith and TwitterService methods.
I haven't solved exactly how I shall display the data but, at runtime, I can see data being passed in.

Retrieving all Users using Google Apps Provisioning API?

I am trying to retrieve all users in domain but it's never work. I never got any error in my code.
I am adding as reference Google.GData.Apps.dll in my project. I am using Google Apps Provisioning API. I am referring these link https://developers.google.com/google-apps/provisioning/
Code :-
string domain = #"<domain name>";
string subs = #"<Authorization code>";
AppsService appService = new AppsService(domain, subs);
// appService.CreateUser("john.jain","James","anderson","james#1234");
// appService.DeleteUser("Amitesh");
appService.RetrieveAllUsers();
The following works for me - RetrieveAllUsers() returns a UserFeed object containing all users. Note that I am using a different constructor for the apps service (using username/password credentials and not OAuth).
this.appsService = new AppsService(credential.Domain, credential.Username, credential.Password);
UserFeed userFeed = this.appsService.RetrieveAllUsers();
// Selecting all users except domain super administrators.
var usernamesInDomain =
(from UserEntry user in userFeed.Entries
where user.Login.Admin != true
select user.Login.UserName).ToList();
What does the returned UserFeed object contain in your case?
This is my solution :-
Google.GData.Apps.UserFeed s = appService.RetrieveAllUsers();
foreach (UserEntry s1 in s.Entries)
{
string username = s1.Login.UserName.ToString();
}

Retrieving information of validated user in WCF

I was following this tutorial at http://www.codeproject.com/Articles/380900/WCF-Authentication-and-Authorization-in-Enterprise
Now I have it logging in and everything, no problems infact it works just like it should. I've even added some cryptography to it using MD5 hashing. But I'm not sure how to get the users information. So when they call the Utility service, how would I query the database for that specific user?
[PrincipalPermission(SecurityAction.Demand, Role = "Read")]
public Data.UserProfiles ViewProfile()
{
using (var context = new DatabaseEntities())
{
var user = context.UserProfiles.SingleOrDefault(u => u.UserName == ???)
return user;
}
}
If you are using the WCF in web application you can store the user details in cookie as the CodeProject article does or you can follow WCF Authentication as here: msdn.microsoft.com/en-us/library/ff405740.aspx
Use below code to get user:
var currentUser = new WindowsPrincipal((WindowsIdentity) System.Threading.Thread.CurrentPrincipal.Identity);

Make Google Plus post through .NET (C#)

Good day all.
Does anyone has a working example how to make Google Plus post through .NET (C#).
I have already tried google and stackoverflow search, but did not manage to find the solution.
I successfully get posts:
public void Post(string text)
{
PlusService plus = new PlusService {Key = "MYVERYSECRETKEY"};
ActivitiesResource ar = new ActivitiesResource(plus, null);
ActivitiesResource.ListRequest list = ar.List("108055870103885325249", new ActivitiesResource.Collection());
ActivityFeed feed = list.Fetch();
string activityKey = "";
foreach (var a in feed.Items)
if (a.Url == "https://plus.google.com/108055870103885325249/posts/EtvvUgn8eKz")
{
activityKey = a.Id;
break;
}
ActivitiesResource.GetRequest get = ar.Get(activityKey);
Activity act = get.Fetch();
var sb = new System.Text.StringBuilder();
sb.AppendLine("Title: " + act.Title);
sb.AppendLine("URL:" + act.Url);
sb.AppendLine("Published:" + act.Published);
sb.AppendLine("By:" + act.Actor.DisplayName);
sb.AppendLine("Annotation:" + act.Annotation);
sb.AppendLine("Content:" + act.Object.Content);
sb.AppendLine("Type:" + act.Object.ObjectType);
sb.AppendLine("# of +1s:" + act.Object.Plusoners.TotalItems);
sb.AppendLine("# of reshares:" + act.Object.Resharers.TotalItems);
}
But I cannot find any method for making posts.
Thanks in advance.
Currently, the Google+ API does not allow writing of posts to a user's activity stream. You can use the moments.insert method in the Google+ REST API to write App Activities to the user's profile, which the user can choose whether to share publicly or to their circles.
You would work with the REST API in a similar manner to other REST APIs in .NET by POSTing to the moments.insert end-point.
This feature is now available to apps that request the https://www.googleapis.com/auth/plus.login scope and specify the type of moments that the app wants to write in the request_visible_actions parameter either in the Google+ Sign-In button or directly in the OAuth query parameters.

Categories

Resources