我的机器人在这个端口上运行:
http://localhost:3979/api/messages
到目前为止,我想使用其他控制器对用户进行身份验证。 我在这里遵循以下代码:
https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Samples/SimpleFacebookAuthBot/Controllers/OAuthCallbackController.cs
所以,在我的情况下,我是这样建立Facebook的链接:
private static string GetOAuthCallBack(ResumptionCookie resumptionCookie, string facebookOauthCallback)
{
var uri = GetUri(facebookOauthCallback,
Tuple.Create("userId", TokenEncoder(resumptionCookie.UserId)),
Tuple.Create("botId", TokenEncoder(resumptionCookie.BotId)),
Tuple.Create("conversationId", TokenEncoder(resumptionCookie.ConversationId)),
Tuple.Create("serviceUrl", TokenEncoder(resumptionCookie.ServiceUrl)),
Tuple.Create("channelId", resumptionCookie.ChannelId),
Tuple.Create("locale", resumptionCookie.Locale ?? "en")
);
return uri.ToString();
}
public static string GetFacebookLoginURL(ResumptionCookie resumptionCookie, string facebookOauthCallback)
{
var redirectUri = GetOAuthCallBack(resumptionCookie, facebookOauthCallback);
var uri = GetUri("https://www.facebook.com/dialog/oauth",
Tuple.Create("client_id", FacebookAppId),
Tuple.Create("redirect_uri", redirectUri),
Tuple.Create("response_type", "code"),
Tuple.Create("scope", "public_profile,email"),
Tuple.Create("state", Convert.ToString(new Random().Next(9999)))
);
return uri.ToString();
}
这很正常。 它以我的oauth方法重定向。然后,在我的控制器到目前为止我有这个:
public async Task<HttpResponseMessage> OAuthCallback([FromUri] string userId, [FromUri] string botId, [FromUri] string conversationId, [FromUri] string channelId, [FromUri] string serviceUrl, [FromUri] string locale, [FromUri] string code, [FromUri] string state, CancellationToken token)
{
// Get the resumption cookie
var address = new Address
(
// purposefully using named arguments because these all have the same type
botId: FacebookHelpers.TokenDecoder(botId),
channelId: channelId,
userId: FacebookHelpers.TokenDecoder(userId),
conversationId: FacebookHelpers.TokenDecoder(conversationId),
serviceUrl: FacebookHelpers.TokenDecoder(serviceUrl)
);
var resumptionCookie = new ResumptionCookie(userId: userId, botId : botId, conversationId : conversationId, channelId : channelId, serviceUrl : serviceUrl, locale : locale);
var accessToken = await FacebookHelpers.ExchangeCodeForAccessToken(resumptionCookie, code, SimpleFacebookAuthDialog.FacebookOauthCallback.ToString());
然后在这种交换令牌的方法中我有这个:
public async static Task<FacebookAcessToken> ExchangeCodeForAccessToken(ResumptionCookie resumptionCookie, string code, string facebookOauthCallback)
{
var redirectUri = GetOAuthCallBack(resumptionCookie, facebookOauthCallback);
var uri = GetUri("https://graph.facebook.com/v2.3/oauth/access_token",
Tuple.Create("client_id", FacebookAppId),
Tuple.Create("redirect_uri", redirectUri),
Tuple.Create("client_secret", FacebookAppSecret),
Tuple.Create("code", code)
);
return await FacebookRequest<FacebookAcessToken>(uri);
}
public async static Task<FacebookAcessToken> ExchangeCodeForAccessToken(ResumptionCookie resumptionCookie, string code, string facebookOauthCallback)
{
var redirectUri = GetOAuthCallBack(resumptionCookie, facebookOauthCallback);
var uri = GetUri("https://graph.facebook.com/v2.3/oauth/access_token",
Tuple.Create("client_id", FacebookAppId),
Tuple.Create("redirect_uri", redirectUri),
Tuple.Create("client_secret", FacebookAppSecret),
Tuple.Create("code", code)
);
return await FacebookRequest<FacebookAcessToken>(uri);
}
最后,http获取对facebook api的请求:
string json;
using (HttpClient client = new HttpClient())
{
json = await client.GetStringAsync(uri).ConfigureAwait(false);
}
但这会引发错误:
{
"error": {
"message": "Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "EY0nNXg3w9U"
}
}
编辑:首先是uri:https://www.facebook.com/dialog/oauth?client_id=217820658623757&redirect_uri=http:%2f%2flocalhost:3979%2fapi%2fOAuthCallback%3fuserId%3dZGVmYXVsdC11c2Vy0%26botId%3dNDlnamdpamw3YTRpbmM4MjFj0%26conversationId%3dN2YzZDc5aTBkNjBjbGhkZm1j0%26serviceUrl%3daHR0cDovL2xvY2FsaG9zdDo1NzU4MQ2%26channelId%3demulator%26locale%3den&response_type=code&scope=public_profile%2cemail&state=3554
和第二个::
https://graph.facebook.com/v2.3/oauth/access_token?client_id=217820658623757&redirect_uri=http:%2f%2flocalhost:3979%2fapi%2fOAuthCallback%3fuserId%3dWkdWbVlYVnNkQzExYzJWeTA1%26botId%3dTkRsbmFtZHBhbXczWVRScGJtTTRNakZqMA2%26conversationId%3dTjJZelpEYzVhVEJrTmpCamJHaGtabTFqMA2%26serviceUrl%3dYUhSMGNEb3ZMMnh2WTJGc2FHOXpkRG8xTnpVNE1RMg2%26channelId%3demulator%26locale%3den&client_secret=4e2729974c5dcd7409d64c64366db18c&code=AQBDFYQAlsryp_qINGLqpFICA5PafRkTsov3ANXPPgezsllDBiCQpAvooiepXlYEFzwOi1UVOaE1CHk_ZXJyp5k4mL_8pGDhzvVVxivBJiY4yJ8PLdkuRcGNu1jU3wHyijX1vs6Df1LOWbYiJfJxsXK243cjQPcfcqd59eUS_yns8FcqGRM1WBMvceYp-VXby9SjOVcqXgRH72hm1s4guv1AppQJ-lQU8YLDyALsrwvBeddzui3F57-NzVWgMAatgfm94NBW8MoTuRxvurWEOvnt03x3CGOIgRdMq0MKvpjYs9dZkioAILJD50C_qt3Jj0A
redirect_uri在两种情况下都是相同的.... 有什么想法??