【问题标题】:How to get Facebook access_token using the OAuth2.0如何使用 OAuth 2.0 获取 Facebook 访问令牌
【发布时间】:2015-05-05 01:27:08
【问题描述】:

我已将 Facebook 登录集成到我的 Web 应用程序中。 当我通过发送所需的详细信息进行请求时,我得到了“代码”参数,然后我必须将获得的“代码”交换为“access_token”。为此,我正在进行 HTTP GET 调用:-

https://graph.facebook.com/oauth/access_token?
    client_id={app-id}
   &redirect_uri={redirect-uri}
   &client_secret={app-secret}
   &code={code-parameter}

我得到的回应是

HTTP/1.1 200 OK [Content-Type: text/plain; charset=UTF-8, Access-Control-Allow-Origin: *, X-FB-Rev: 1624746, Pragma: no-cache, Cache-Control: private, no-cache, no-store, must-revalidate, Facebook-API-Version: v1.0, Expires: Sat, 01 Jan 2000 00:00:00 GMT, X-FB-Debug: XM3kCi/2T3bPfe2/QRmn08rBwHTn7SkY8iADhkirVCZ4HOKPGH45zK8WWq0/5+KDFdUWwIn7blnjYfy1py6jrQ==, Date: Wed, 04 Mar 2015 06:25:17 GMT, Connection: keep-alive, Content-Length: 245]

在响应中,我没有获得进一步进入应用程序所需的 access_token 参数。 根据 Facebook 文档的响应应该看起来像

access_token={access-token}&expires={seconds-til-expiration}

非常感谢您对此的任何帮助。

【问题讨论】:

    标签: facebook facebook-graph-api access-token facebook-oauth


    【解决方案1】:

    终于设法获得答案的解决方案。Facebook 提供了 access_token,但需要使用 entityutils 对象检索它,然后转换为 UTF-8 类型。这是对我有用的代码,我能够得到来自 Facebook API 响应的 access_token 对象。

    HttpGet request = new HttpGet(FBAPI_HITURL.toString());
    HttpResponse response = httpClient.execute(request);
    HttpEntity entity = response.getEntity();
    String responseString = EntityUtils.toString(entity, "UTF-8");
    

    responseString 将包含格式为“access_token={access-token}&expires={seconds-til-expiration}”的响应

    【讨论】:

      猜你喜欢
      • 2011-02-28
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 2012-12-10
      • 2023-01-23
      • 2019-09-10
      相关资源
      最近更新 更多