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

问题描述 投票:0回答:1

我正在使用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);
}

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

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

c# asp.net-web-api foursquare sharpsquare
1个回答
5
投票

基本上这种认证是一个两步过程。首先,您需要使用GetAuthenticateUrl()调用生成身份验证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");
}

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

更新:事实证明,主题入门者对“无用户”访问感兴趣(请参阅FourSquare dev page here)。实际上,对于这些操作,您不需要获取访问令牌,您只需要APP ID和SECRET。看着SharpSquare implementation,我注意到有一个overloadbool unauthenticated参数:

private FourSquareSingleResponse<T> GetSingle<T>(string endpoint, bool unauthenticated) where T : FourSquareEntity

这确实只需要app id / secret when calling API

调用以下公共方法时,此参数设置为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)

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

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

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

    return View(venues);
}
© www.soinside.com 2019 - 2024. All rights reserved.