I'm trying to learn c# and asp.net webforms and am working on an application that is similar to how reddit or facebook might function. Users have the ability to create accounts, send and accept friend requests, leave wall posts etc.
I have implemented the ability to send a friend request, but am stuck on the part where a user can accept the request. Here is the c#:
using (Database.DataModelDataContext db = new Database.DataModelDataContext())
{
//Guid RequesterId = new Guid(Request.QueryString["ID"]);
CRANK.Database.Friendship RequestAccept = new Database.Friendship
{
ID = Guid.NewGuid(),
UserA = base.UserId,
UserB =
};
db.Friendships.InsertOnSubmit(RequestAccept);
db.SubmitChanges();
}
So as you can see, UserA would be added to the database with the logged-in users ID. I can't figure out how to get the information for UserB or the person that the original friend request was sent from. I'm assuming we can somehow grab it from the code in asp.net.
So the ID was stored in a CommandArgument. I used this code:
Guid userRequested = Guid.Parse(e.CommandArgument.ToString());
UserB = userRequested
Related
I'm working on a ASP.NET MVC 5 with Entity Framework (version 6.0) application.
I have added the simple google login, that saves the google email with the user on registration. How do I also get the profile picture of the Google+ user when they login and cast it in a view?
Google Plus API for developers allows you to fetch public data from Google+.
Followed by detail tutorial of all the necessary steps one need to perform to successfully fetch public data from Google+.
Google implies a limit to the usage of Google+ API - Each developer has a quota. We will see about that when we will discuss Google API console.
Google uses OAuth2.0 protocol to authorize your application when it tries to access user data.
It mostly uses standard HTTP method by means of RESTful API design to fetch and manipulate user data.
Google uses JSON Data Format to represent the resources in the API.
Step1: Generate an API key through Google API Console.
Step2: used GoogleOAuth2AuthenticationOptions which means you'll need to set
up a project at https://console.developers.google.com/project first to get a ClientId and ClientSecret.
At that link (https://console.developers.google.com/project), create a project and then select it.
Then on the left side menu, click on "APIs & auth".
Under "APIs", ensure you have "Google+ API" set to "On".
Then click on "Credentials" (in the left side menu).
Then click on the button "Create new Client ID".
Follow the instructions and you will then be provided with a ClientId and ClientSecret, take note of both.
var googleOptions = new GoogleOAuth2AuthenticationOptions()
{
ClientId = [INSERT CLIENT ID HERE],
ClientSecret = [INSERT CLIENT SECRET HERE],
Provider = new GoogleOAuth2AuthenticationProvider()
{
OnAuthenticated = (context) =>
{
context.Identity.AddClaim(new Claim("urn:google:name", context.Identity.FindFirstValue(ClaimTypes.Name)));
context.Identity.AddClaim(new Claim("urn:google:email", context.Identity.FindFirstValue(ClaimTypes.Email)));
//This following line is need to retrieve the profile image
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:google:accesstoken", context.AccessToken, ClaimValueTypes.String, "Google"));
return Task.FromResult(0);
}
}
};
app.UseGoogleAuthentication(googleOptions);
//get access token to use in profile image request
var accessToken = loginInfo.ExternalIdentity.Claims.Where(c => c.Type.Equals("urn:google:accesstoken")).Select(c => c.Value).FirstOrDefault();
Uri apiRequestUri = new Uri("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + accessToken);
//request profile image
using (var webClient = new System.Net.WebClient())
{
var json = webClient.DownloadString(apiRequestUri);
dynamic result = JsonConvert.DeserializeObject(json);
userPicture = result.picture;
}
OR
var info = await signInManager.GetExternalLoginInfoAsync();
var picture = info.ExternalPrincipal.FindFirstValue("pictureUrl");
ExternalLoginCallback method I check for which login provider is being used and handle the data for Google login.
Go through the link to get more information.
https://developers.google.com/identity/protocols/OAuth2
I have tried it its working.
I'm developing a public website and what I want to do is pretty straightforward, but I'm pulling my hair out trying to get everything working right.
I administer an open Facebook group and I want to display the public facebook events of this group on my website.
I can't seem to figure out how to setup my authentication so that I can access the event data. Here is my code for using my application to get an auth token:
var fb = new FacebookClientWrapper();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = AppSettings.AppID,
client_secret = AppSettings.AppSecret,
grant_type = "client_credentials"
});
fb.AccessToken = result.access_token;
I know this works fine because I can access some information - for example, if I access a specific event by its ID, I can retrieve that information.
The problem occurs when I try to retrieve a list of events with fields within a date range:
[HttpGet]
public object GetEventDetails(string unixStartDateTime, string unixEndDateTime)
{
var parms = new Dictionary<string, object>();
parms.Add("fields", new[] { "id","name","description","start_time","venue" });
if (!String.IsNullOrEmpty(unixStartDateTime)) { parms.Add("since", unixStartDateTime); }
if (!String.IsNullOrEmpty(unixEndDateTime)) { parms.Add("until", unixEndDateTime); }
var eventsLink = String.Format(#"/{0}/events", AppSettings.GroupID);
return ObjectFactory.GetInstance<IFacebookClient>().Get(eventsLink,parms);
}
(I'm aware that even if this did succeed, the return value wouldn't be serializable - I'm not concerned about that quite yet).
This GET request returns the following message:
(OAuthException - #102) A user access token is required to request this resource.
So the message is quite clear: I need a user access token to get the data I've requested. The question is - what is the best way to do this? Can I give my application a certain permission to read this data? I've looked over all the permissions available to apps, but I don't see one that would do the trick.
I don't want to require people to log onto Facebook to look at public event data, and I love the idea of allowing people with no technical experience to essentially update the website content by posting Facebook events to the group. Right now, I have to duplicate anything they do.
I would think this kind of application would be very common, but no matter what I've read or tried, I can't quite find an example of the same thing that works.
From the docs at https://developers.facebook.com/docs/graph-api/reference/v2.0/group/events you need
A user access token for a member of the group with user_groups permission.
To avoid the hassle, you could create such an Access Token via the Graph Explorer and then store it in your application. Remember to exchange that Access Token to a long-lived one (https://developers.facebook.com/docs/facebook-login/access-tokens/#extending), and that you have to renew the Access Token every 60 days afterwards.
For the life of me I can no figure out where to get the SessionID out of the service so that I can use it in a future call to recreate the same session.
I am currently using the 2013_1_0 version of the wsdl in C#
When referencing the older documents SuiteTalkWebServicesPlatformGuide_2011.1.pdf on page 78 (Reusing Session IDs During Login) they talk about this.
Any Suggestions?
Here is my sample code for logging into NetSuite without using a previous SessionID...
// invoke the login operation
Passport passport = new Passport();
passport.account = _sNetSuiteAccount;
passport.email = _sNetSuiteUserName;
RecordRef role = new RecordRef();
role.externalId = _sNetSuiteRole;
passport.role = role;
passport.password = _sNetSuitePassword;
status = service.login(passport).status;
// Confirm login was a success
// Process response
if (status.isSuccess == true)
{
_bIsNetSuiteAuthenticated = true;
}
Additionally I have yet to find sample code for using the SessionID to re-create the service. So any suggestion there would also be welcomed.
Thank you!
In talking with NetSuite what I desire to do is impossible and the SessionID is stored as a private variable and handled by the .NET code.
I am currently seeking an alternative to a persistent session so when code base is called multiple times that the user will not need to be prompted for each iteration to enter their NetSuite cridentials. I will update this answer as more details come available on this.
I have some doubts:
I want to show the photos from my company facebook page on my company web site using C# SDK.
How can I do that with an access token which never expires? Or why my app id and app secret is not enough?
var fb = new FacebookWebClient("app_id", "app_secret");
If I only use this it says:
An active access token must be used to query information about the current user.
Basically I see non-sense to ask for a valid access token because it's not about to interact with any of my visitors' page but just simply show my photos from my company facebook page to them.
UPDATE: To get my app token I am doing:
var oauthClient = new FacebookOAuthClient
{
AppId = "app_id",
AppSecret = "app_secret"
};
dynamic result = oauthClient.GetApplicationAccessToken();
accessToken = result.access_token;
But now, how I can show photos from my account?
If I do it like:
var fb = new FacebookWebClient(accessToken);
//Get the album data
dynamic albums = fb.Get("app_id/albums");
I receive an empty JSON but I am sure that my account on facebook has several albums.
Tokens can and do expire. You must have code in place to handle when it does.
Your appid and app secret are not enough because at least one of the page admins MUST grant access to the page to get photo data via the Graph API.
Remember, the API is not only Facebook's sandbox, but they also are the ones who built it, so you're going to have to play by their rules.
I need help on oAuth authentication to facebook. I am able to do the steps that facebook API mentioned and successfully authenticated, get my profile information as well as post to my news feed.
I want my application to login automatically rather than routing to Facebook login page on behalf of a user(i.e. get credentials from db and do auto oauth) . So that the user can type a message on application and click on button should submit a post to facebook page.
Thanks
What you need to get is an access token. This is a key which allows you continued access to the account until your the token expires or the user cancels it.
When you first authenticate if you have set the required permissions you get an authtoken which you can store and use to initiale further calls.
var client = new FacebookClient("my_access_token");
dynamic result = client.Get("19292868552_118464504835613");
string id = result.id;
string fromName = result.from.name;
string fromCategory = result.from.category;
string message = result.message;
int likes = result.likes;
foreach (dynamic comment in result.comments.data) {
string commentId = comment.id;
string commentMessage = comment.message;
}
see this article for details about the process:
http://benbiddington.wordpress.com/2010/04/23/facebook-graph-api-getting-access-tokens/