无法获取响应代码
Can't get response code
我想获取用户AccessToken。
当用户点击 Register/Login 按钮时:
$.ajax({
url: 'Register',
type: 'GET',
data: { m:true },
contentType: 'application/json; charset=utf-8',
success: function (response) {
if (response.success) {
} else {
}
},
error: function () {
}
});
我的注册方式:
Uri uri = new Uri(FacebookHelpers.GetFacebookLoginURL());
var res = await FacebookHelpers.FacebookRequest<object>(uri).ConfigureAwait(false);
在 res 中,我想从 FacebookRequest 获得结果以获取代码,而不是将该代码交换为访问令牌。
我的 GetFacebookLoginUrl 函数:
var redirectUri = "https://www.facebook.com/connect/login_success.html";
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)))
);
在我的 FacebookRequest 函数中:
public static async Task<T> FacebookRequest<T>(Uri uri)
{
string json;
using (HttpClient client = new HttpClient())
{
json = await client.GetStringAsync(uri).ConfigureAwait(false);
}
try
{
var result = JsonConvert.DeserializeObject<T>(json);
return result;
}
catch (JsonException ex)
{
throw new ArgumentException("Unable to deserialize the Facebook response.", ex);
}
}
当 FacebookRequest 函数尝试从提供的 URL 中获取代码时会出现此问题。
Unexpected character encountered while parsing value: <. Path '', line
0, position 0.
Json 响应包含调用方法的相同页面。
FacebookRequest 没有 returns 代码,而是 returns 成功。
有什么想法吗?
目标:我想从我的数据库中获取用户并 link 它在我的机器人中的帐户。或者,如果用户不存在,请先注册然后 link 使用机器人。
您无法在后台加载 Facebook 登录对话框 - 您必须将用户重定向到该地址。它必须在用户的浏览器中调用;如果您尝试在后台请求它,那是行不通的。
至于你是得到代码还是返回访问令牌:你可以指定你想要直接返回访问令牌——但在这种情况下,令牌是在你的重定向 URI 的片段部分传递的,这意味着你只能使用客户端代码从那里获取它。我想这不是一个选择。 (如果您需要更多信息,此处描述了参数 response_type
和可能的值,https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#logindialog)
我想获取用户AccessToken。
当用户点击 Register/Login 按钮时:
$.ajax({
url: 'Register',
type: 'GET',
data: { m:true },
contentType: 'application/json; charset=utf-8',
success: function (response) {
if (response.success) {
} else {
}
},
error: function () {
}
});
我的注册方式:
Uri uri = new Uri(FacebookHelpers.GetFacebookLoginURL());
var res = await FacebookHelpers.FacebookRequest<object>(uri).ConfigureAwait(false);
在 res 中,我想从 FacebookRequest 获得结果以获取代码,而不是将该代码交换为访问令牌。
我的 GetFacebookLoginUrl 函数:
var redirectUri = "https://www.facebook.com/connect/login_success.html";
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)))
);
在我的 FacebookRequest 函数中:
public static async Task<T> FacebookRequest<T>(Uri uri)
{
string json;
using (HttpClient client = new HttpClient())
{
json = await client.GetStringAsync(uri).ConfigureAwait(false);
}
try
{
var result = JsonConvert.DeserializeObject<T>(json);
return result;
}
catch (JsonException ex)
{
throw new ArgumentException("Unable to deserialize the Facebook response.", ex);
}
}
当 FacebookRequest 函数尝试从提供的 URL 中获取代码时会出现此问题。
Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
Json 响应包含调用方法的相同页面。 FacebookRequest 没有 returns 代码,而是 returns 成功。
有什么想法吗?
目标:我想从我的数据库中获取用户并 link 它在我的机器人中的帐户。或者,如果用户不存在,请先注册然后 link 使用机器人。
您无法在后台加载 Facebook 登录对话框 - 您必须将用户重定向到该地址。它必须在用户的浏览器中调用;如果您尝试在后台请求它,那是行不通的。
至于你是得到代码还是返回访问令牌:你可以指定你想要直接返回访问令牌——但在这种情况下,令牌是在你的重定向 URI 的片段部分传递的,这意味着你只能使用客户端代码从那里获取它。我想这不是一个选择。 (如果您需要更多信息,此处描述了参数 response_type
和可能的值,https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#logindialog)