私は一種の似た何かをした、ときユーザーが私のmvcアプリケーションの "共有"ボタンをクリックすると、彼の壁に何かが投稿されます。 oauthダイアログを使用する問題は、ユーザーがログインしてアプリケーションのアクセス許可を受け入れるために、ブラウザをFacebookのサイトにリダイレクトすることです。 "シェア" ボタンを
、私はこのURLにリンクさ:
<a href=""https://www.facebook.com/dialog/oauth?client_id=[YOUR_APP_ID]&redirect_uri=[THE_REDIRECT_PAGE]/&scope=publish_stream"">
<img src='@Url.Content("~/Images/facebook_share.png")' alt="Share on Facebook!" style="height:28px" />
</a>
YOUR_APP_IDは、自分のFacebookのアプリケーションIDです。 THE_REDIRECT_PAGEは、ユーザーがログインしてアクセス許可を受け入れると、Facebookが自動的にリダイレクトするサイトのパブリックページです。 facebookがリダイレクトされると、 "code"というクエリ文字列のパラメータが追加されます。 注:リダイレクトページは "/"で終わらなければなりません。ドキュメントで終わることはできません。そうでないと動作しません。
ユーザーがリクエストを受け入れたら、アクセスコードと呼ばれる別のコードをFacebookの壁に掲示する必要があります。
このコードは、リダイレクトページにある:
public ActionResult Index(string code)
{
string fbAuthCode = Request["code"]; //The authorization code.
string fbAppId = "XXXXXXX"; //Your fb application id.
string fbSecretAppId = "XXXXXXXXXXXXXXXXXXXXX"; //Your fb secret app id, it is found on the fb application configuration page.
string redirectUrl = string.Format("[THE_REDIRECT_PAGE]", locationPointId, entryLocationId); //The redirect url. THIS MUST BE THE EXACT SAME REDIRECT URL USED ON THE JAVASCRIPT LINK!
string fbUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + fbAppId + "&redirect_uri=" + redirectUrl + "&client_secret=" + fbSecretAppId + "&code=" + fbAuthCode; //Url used to post.
string accessToken = string.Empty;
try
{
WebClient client = new WebClient();
using (Stream stream = client.OpenRead(fbUrl))
using (StreamReader reader = new StreamReader(stream))
{
accessToken = reader.ReadToEnd().Split('&')[0].Replace("access_token=", string.Empty);
reader.Close();
}
}
catch (Exception ex)
{
throw new Exception("An error ocurred while trying to get the fb token in " + fbUrl, ex);
}
あなたがアクセストークンを取得したら、あなたはユーザー壁に投稿することができます:
string postUrl = "https://graph.facebook.com/me/feed";
string postParameters;
postParameters = string.Format("message={0}&picture={1}&name={2}&caption={2}&description={3}&link={4}&access_token={5}",
"[Message]",
"[PictureUrl]",
"[Name]",
"[Caption]",
"[Link]",
accessToken);
try
{
System.Net.WebRequest req = System.Net.WebRequest.Create(postUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postParameters);
req.ContentLength = bytes.Length;
using (System.IO.Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
using (WebResponse resp = req.GetResponse())
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
ViewBag.PostResult = sr.ReadToEnd().Trim();
sr.Close();
}
os.Close();
}
}
catch (Exception ex)
{
throw new Exception("An error ocurred while posting data to the user's wall: " + postUrl + "?" + postParameters, ex);
}
return RedirectToAction(XXXXXXXXXXXxx....); //Then i redirect to another page.
あなたは、私が投げる例外にそれを見ることができます投稿されたURL(デバッグ用)。 そのURLを使用すると、通常facebook Graph API ExplorerまたはLinterにアクセスし、実際のエラーを確認できます。
これがまさにあなたが望むものなのかどうかわかりませんが、それはあなたにキックオフを願っています... オープングラフのFacebookドキュメントはあまり良くありませんので、これで数日間苦労しました。カール使用していないこと:)
https://developers.facebook.com/docs/opengraph/tutorial/ https://developers.facebook.com/docs/opengraph/
を私たちのために、少なくとも、それがお役に立てば幸いです。 MT。