【问题标题】:Obtain an App Access Token with Android FacebookSDK v3使用 Android FacebookSDK v3 获取应用访问令牌
【发布时间】:2013-02-28 05:32:19
【问题描述】:

这是我的第一个主题,希望你能帮助我(请用我的英语,因为我的第一语言是法语)thx :))

我有一个问题,我正在尝试使用 Facebook Android Facebook SDK v3 获取应用访问令牌,但到目前为止我没有成功。

我在互联网上看到,要获取应用令牌,我们需要访问该链接:

https://graph.facebook.com/oauth/access_token client_id=*************&client_secret=***********************&%20grant_type=client_credentials

所以我做了一个请求对象来获取应用令牌:

Bundle bundle = new Bundle();
bundle.putString("client_id", appId);
bundle.putString("client_secret", appSecret);
bundle.putString("grant_type", "client_credentials");

Request request = new Request(null, "oauth/access_token", bundle, HttpMethod.GET);
Response resp = request.executeAndWait();

对象“响应”返回给我一个 GraphObject,如下所示: GraphObject{graphObjectClass=GraphObject, state={"FACEBOOK_NON_JSON_RESULT":"access_token"}}

但请求返回的响应中不包含访问令牌。

但是,当我在浏览器中启动链接时,我会看到如下页面: access_token=[the_access_token]

为什么我不能得到与响应相同的东西? 难道我做错了什么?有人有解决办法吗?

非常感谢!

【问题讨论】:

  • 我认为这个端点的响应是 not JSON 的事实可能是问题 - 如果您的请求对象期望它是 JSON,但得到其他东西,它可能会失败. FACEBOOK_NON_JSON_RESULT 消息也表明了这一点。

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


【解决方案1】:

我有同样的问题。

访问令牌不是 JSON 类型。所以你不能使用facebook api的'request'和'response'。 您将在链接下找到解决方案。

Show this link

或见代码下


public static void getAppAccessToken(){
    String url = "https://graph.facebook.com/oauth/access_token?client_id="+CLIENT_ID+"&client_secret="+CLIENT_SECRET+"&grant_type=client_credentials";
    InputStream is;
    String result;
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);

    try{
        HttpResponse response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();
        is = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
             sb.append(line);
        }   

        is.close();
        result=sb.toString();
        result = result.split("=")[1];
        appAccessToken = result;
    }
    catch(Exception e)
    {

    }
}

【讨论】:

  • 永远不要在其他问题中讨论您的问题,红色常见问题解答以便更好地理解,如果您回答,请提及。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 2015-07-19
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
相关资源
最近更新 更多