如何在Web API中使用C#获取Foursquare OAuth请求代码?

Cha*_*eus 0 c# foursquare asp.net-web-api sharpsquare

我正在使用SharpSquare进行Foursquare API的C#建模,我在第1步陷入困境.

  1. 从Foursquare获取"代码"
  2. 根据返回网址和"代码"获取访问令牌
  3. 查询API

SharpSquare文档显示了这一点:

if (Request["code"] != null) {
    sharpSquare.GetAccessToken(redirectUri, Request["code"]);
    ...
}
else {
    HyperLink.NavigateUrl = sharpSquare.GetAuthenticateUrl(redirectUri);
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试从ASP.NET Web API方法中查询Foursquare API .我不能这样做: HyperLink.NavigateUrl = sharpSquare.GetAuthenticateUrl(redirectUri);

我该怎么做才能获得"代码"?(我已经尝试了各种WebClient()和WebRequest/WebResponse尝试,只是不能让它工作).

avs*_*099 5

基本上这种认证是一个两步过程.首先,您需要使用GetAuthenticateUrl()call 生成身份验证URL ,并将用户重定向到该URL.用户成功进行身份验证后,FourSquare将调用您提供的重定向URL - 传递code参数 - 您需要通过GetAccessToken()调用来交换访问令牌.

这是一个示例MVC代码:

public ActionResult UserClicksAuthenticate()
{
    var redirectUri = Request.Url.Authority + this.Url.Action("AuthorizeCallback", new {userCode = "userCode"});
    var sharpSquare = new SharpSquare(clientId, clientSecret);
    var authUrl = sharpSquare.GetAuthenticateUrl(redirectUri);

    return new RedirectResult(authUrl, permanent: false);
}

public ActionResult AuthorizeCallback(string code, string userCode)
{
    var redirectUri = Request.Url.Authority + this.Url.Action("AuthorizeCallback", new { userCode = userCode });

    var sharpSquare = new SharpSquare(clientId, clientSecret);    
    var accessToken = sharpSquare.GetAccessToken(redirectUri, code);

    // need this in order to make calls to API
    // it's redundant because token is already set in GetAccessToken() call but it helps to understand the workflow better. 
    sharpSquare.SetAccessToken(accessToken);

    List<VenueHistory> venues = sharpSquare.GetUserVenueHistory();

    return View("Index");
}

public ActionResult GetVenues()
{
    var sharpSquare = new SharpSquare(clientId, clientSecret, appToken);    

    List<VenueHistory> venues = sharpSquare.GetUserVenueHistory();

    return View("Index");
}
Run Code Online (Sandbox Code Playgroud)

userCode上面只是您可以在重定向网址中传递的任意字符串,因此可以恢复回调函数中的用户上下文.例如,这可以是页面代码,用户ID或事件名称 - 无论您的逻辑需要什么.

更新:事实证明,主题入门者对"无用户"访问感兴趣(请参阅此处的FourSquare开发页面).实际上,对于这些操作,您不需要获取访问令牌,您只需要APP ID和SECRET.纵观SharpSquare实现,我注意到,有一个存在过载bool unauthenticated参数:

private FourSquareSingleResponse<T> GetSingle<T>(string endpoint, bool unauthenticated) where T : FourSquareEntity
Run Code Online (Sandbox Code Playgroud)

在调用API时确实只需要app id/secret .

true调用以下公共方法时,此参数设置为:

public Venue GetVenue(string venueId)
public List<Venue> SearchVenues(Dictionary<string, string> parameters)
public List<Checkin> GetVenueHereNow(string venueId, Dictionary<string, string> parameters)
public List<Tip> GetVenueTips(string venueId, Dictionary<string, string> parameters)
public List<Photo> GetVenuePhotos(string venueId, Dictionary<string, string> parameters)
public List<Link> GetVenueLinks(string venueId)
public Tip GetTip(string tipId)
public Special GetSpecial(string specialId)
Run Code Online (Sandbox Code Playgroud)

这意味着如果要在WebAPI操作中调用任何这些方法,则不需要访问令牌:

public ActionResult SearchVenues()
{
    var sharpSquare = new SharpSquare(clientId, clientSecret);    

    List<VenueHistory> venues = sharpSquare.SearchVenues(<<params>>);

    return View(venues);
}
Run Code Online (Sandbox Code Playgroud)