How to post using PostTaskAsync in Facebook C# SDK? - c#

I'm using the Facebook C# SDK to authorize my app login and post on the user's timeline. So far the login feature works and permissions are authorized, but when my code gets to the PostTaskAsync method, my app crashes with an error stating:
The program '[1020] LC Points.WindowsPhone.exe' has exited with code 0 (0x0).
I've been following this example on SO but their is no Api method available in the SDK I'm using.
Does anyone know how to debug or resolve this posting issue?
I've submitted the publish_actions for review on the Facebook developer console, but don't think that's the reason the app is crashing.
This is the ShareApp Task I'm using to call the login and posting code:
private async Task ShareApp()
{
//Facebook app id
var clientId = "573586446116744";
//Facebook permissions
var scope = "public_profile, email, publish_actions";
var redirectUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString();
var fb = new FacebookClient();
var app = new FacebookClient(fb.AccessToken);
var loginUrl = fb.GetLoginUrl(new
{
client_id = clientId,
redirect_uri = redirectUri,
response_type = "token",
scope = scope
});
Uri startUri = loginUrl;
Uri endUri = new Uri(redirectUri, UriKind.Absolute);
WebAuthenticationBroker.AuthenticateAndContinue(startUri, endUri, null, WebAuthenticationOptions.None);
//Code to post app on the user's timeline after button click..crashes when program execution comes to this code:
var postArgs = new Dictionary<string, object>();
postArgs["link"] = "http://allaboutwindowsphone.com/software/developer/Brian-Varley.php";
postArgs["name"] = "More from BV Apps..";
postArgs["message"] = "I'm using LC Points to calculate my Leaving Cert Points!";
await app.PostTaskAsync("/me/feed", postArgs);
//app.Api("/me/feed", postArgs, HttpMethod.Post); //no Api method available which is why I've used PostTaskAsync instead.
}
This is the link to the repo on my GitHub for reference

Related

Refresh token missing in Google Oauth response file

I'm implementing Google OAuth in ASP.Net MVC application using Google's OAuth .Net library. Below is my code.
IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer {
ClientSecrets = new ClientSecrets {
** ClientId ** , ** ClientSecret **
},
DataStore = new FileDataStore( ** responsepath ** , true),
Scopes = new [] {
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/gmail.send"
},
Prompt = "select_account"
});
var userId = "user";
var uri = Request.Url.ToString();
var code = Request["code"];
if (code != null) {
var token = flow.ExchangeCodeForTokenAsync(userId, code, uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;
var oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, userId, Request["state"]).Result;
Response.Redirect(oauthState);
} else {
var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(userId, CancellationToken.None).Result;
if (result.RedirectUri != null) {
Response.Redirect(result.RedirectUri);
}
}
When user click's Google sign-in button, my page is redirected to Google authentication page. After successful authentication, my page is displayed again. When I check the responsepath, below file is created which contains access token, expiry time, etc.
Google.Apis.Auth.OAuth2.Responses.TokenResponse-user
When I run the above code locally in my visual studio debugging environment (IIS express), the above response file has "refresh_token" in it. When the same code is deployed in production environment (IIS), the "refresh_token" is missing is response file. I would like to know the reason behind it since I need refresh token for further processing.
Note: In both the cases, I revoked the application's access from my Google account before trying. So, this is not about "refresh_token" will be sent only for the first time.
Adding prompt=consent parameter while sending request to Google gives refresh token every time without fail.

How to verify code sent in passwordless authentication in Auth0 using API?

We can start the passwordless flow using the Auth0 .NET client like this:
var apiClient = new AuthenticationApiClient(new Uri(_appCredentials.Domain));
var result = await apiClient.StartPasswordlessEmailFlowAsync(new PasswordlessEmailRequest
{
ClientId = _appCredentials.ClientId,
Email = email,
Type = PasswordlessEmailRequestType.Code
});
However, verifying the code
var token = await apiClient.GetTokenAsync(new AuthorizationCodeTokenRequest
{
ClientId = _appCredentials.ClientId,
ClientSecret = _appCredentials.ClientSecret,
Code = code
});
Doesn't work. It doesn't seem that actually verifying the passwordless code is supported via their API. Is that correct? How do we verify the code from our end without embedding the Universal Login widget into our site?

Using Facebook SDK to publish on user feed / wall

I'm trying to understand if it is possible to post on the users' wall from my Facebook application.
At the moment I have:
One Facebook app with the permission to write on the users' wall
A BackEnd with Fairbooks SDK Installed
Actually I'm following this approach:
public static string GetToken()
{
var fb = new Facebook.FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = APP_ID,
client_secret = APP_S,
grant_type = "client_credentials"
});
return result.access_token;
}
public static void Post(string Message, long UserID)
{
var token = GetToken();
var client = new FacebookClient(token);
client.Post("/" + UserID + "/photos", new { url = "url", caption = Message });
}
My final goal is to post on facebook when the user interact with my API without client-side popups. Is this possible?
This line of code calls for an application access token
dynamic result = fb.Get("oauth/access_token", new
{
client_id = APP_ID,
client_secret = APP_S,
grant_type = "client_credentials"
});
It makes no sense to use this if you haven't first retrieved a user access token in advance. Only then can you make calls on behalf of the user.
My final goal is to post on facebook when the user interact with my API without client-side popups. Is this possible?
This will never be possible by design. All 3rd party applications must invoke a client-side activity for the user in some format. It cannot be automated.

Trying to post message to facebook using windows client app

I am using visual studio 2013 .NET 4.5 and C# with a windows forms client application, I am trying to post a message to my own Facebook managed page. I have created a new Facebook application, but not yet published it, I am unsure of the settings, but I have the app_id, app_secret and from debugging I have the access_token.
I used a piece of code to open a browser window to login with permissions and this was successful/
I am getting the following error:
OAuthException - #200) (#200) The user hasn't authorized the application to perform this action
The code I am using is below:
var fb = new FacebookClient("AUTH_CODE")
dynamic parameters = new ExpandoObject();
parameters.message = "hello tweet tweet";
dynamic result = fb.Post("SITE_ID/feed", parameters);
var id = result.id;
for logging in I am using:
var client = new FacebookClient();
client.AppSecret = app_secret;
client.AppId = app_id;
client.AccessToken = "AUTH_CODE";
var fbLoginUri = client.GetLoginUrl(new
{
client_id = app_id,
redirect_uri =
"https://www.facebook.com/connect/login_success.html",
response_type = "code",
scope = "manage_pages,offline_access,publish_stream,publish_actions"
});
System.Diagnostics.Process.Start(url);
I am already logged in and the message on the browser shows SUCCESS...
The DLL that I have referenced is the latest v6.0 Facebook C# SDK
Do I need to setup the Facebook app so that it has permissions to post onto my managed wall and if so how do I do it.
I hope someone out there can help, I have been looking at this for days and I need a little help.
PS:
I have also tried the following code:
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = app_id,
client_secret = app_secret,
grant_type = "client_credentials",
scope = "manage_pages,offline_access,publish_stream"
});
var app_token = result.access_token;
var client = new FacebookClient(app_token);
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.natiska.com/article.html";
parameters.picture = "http://www.natiska.com/dav.png";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
//446533181408238 is my fan page
client.Post("/446533181408238/feed", parameters);
Paul.
It seems that the request code from your access token request is missing which probably causes this error, see this question for more details on this.

Malformed access token 190 error

when trying to redirect to gain a new access token an error occurred saying :
(OAuthException - #190) Malformed access token AQClEC8NbS1tbO4_HzqqZwVy2Inrk5I7vBpszPDPN0dVOV0ekurwNEpqzlWe2hQzl2KNZ5BwiSvL16bDPhR5IxFeJXEXBdM67uky7iwKbYKIVoRF__iyayyXBgcIp939gEZNtCUxyO4ZKZHQ9xmEwDstaOgq_bAvtygwv3YUeEYD9wftsytMZYwVxDO7rjq-2i3-XCYj643-6_1yKZ0dDahAB00iBqJqw1xiZ-l1kzStmA_pwMwO5dtBHZozp-L2hp0JEeTL0Fhc5CG1zbJ8Om6kyKBWyZuc0AtOrdSjgcBQBv9hQSF5pwsMZvFDGcjiwSc
what possibly could be the problem i am doing this method to redirect :
string redirecturl = #"https://graph.facebook.com/oauth/authorize?client_id=507061112747022&redirect_uri=http://localhost:63695/FacebookChatApi/Default.aspx";
Response.Redirect(redirecturl);
and i am reading the access token as follow :
string accessToken=Request["code"];
var client = new FacebookClient(accessToken); // here is where the error occurred
Is this the right method to get a client access token ?
why this error is occurring when reading the code value
according to Facebook Debug tool, your token is not a Token but a URL instead: https://developers.facebook.com/tools/debug/og/object?q=AQClEC8NbS1tbO4_HzqqZwVy2Inrk5I7vBpszPDPN0dVOV0ekurwNEpqzlWe2hQzl2KNZ5BwiSvL16bDPhR5IxFeJXEXBdM67uky7iwKbYKIVoRF__iyayyXBgcIp939gEZNtCUxyO4ZKZHQ9xmEwDstaOgq_bAvtygwv3YUeEYD9wftsytMZYwVxDO7rjq
I'm not sure who you get it, and why you get it instead of access token - you did not show enough code. From what I saw, I believe you generate your redirecturl incorrectly. Below is a piece of code from one of my project - please try it and let me know if it works for you:
// 1. redirect user to the login page:
var redirectUri = "http://localhost:63695/FacebookChatApi/Default.aspx";
var service = new FacebookClient();
var loginUrl = service.GetLoginUrl(new {
client_id = ConfigurationProvider.FacebookAppId,
client_secret = ConfigurationProvider.FacebookAppSecret,
redirect_uri = redirectUri,
response_type = "code",
scope = "manage_pages, publish_actions, user_photos, publish_stream" // Add other permissions as needed
});
Response.Redirect(loginUrl);
// 2. And when user authorizes, get the access token
// NOTE: code executes on FacebookChatApi/Default.aspx page
var redirectUri = "http://localhost:63695/FacebookChatApi/Default.aspx"; // must be the same as in the login call!
var fb = new FacebookClient();
dynamic result = fb.Post("oauth/access_token", new
{
client_id = ConfigurationProvider.FacebookAppId,
client_secret = ConfigurationProvider.FacebookAppSecret,
redirect_uri = redirectUri,
code = code
});
var accessToken = result.access_token;

Categories

Resources