【问题标题】:How do i get Instagram Code for Oauth authorisation?我如何获得 Instagram 代码以进行 Oauth 授权?
【发布时间】:2014-07-30 12:46:37
【问题描述】:

我现在正在为 instagram 制作 Java 桌面应用程序,但我不知道如何从回调 url 获取代码。我的应用是桌面应用,所以我的 REDIRECT-URI 是 http://instagram.com。所以在我的应用程序中,我向https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code 发送请求并被重定向到instagram.com?code=CODE。也忘了提一下,我曾经得到一个代码,我给了所有权限。所以现在我提出请求,我用代码直接重定向到 URL。 如何从Java程序中的回调uri获取代码?这是我的代码:

static void GetCode()
{
    try {
        String url = "https://api.instagram.com/oauth/authorize/?client_id=" + ClientId + "&redirect_uri=" + BackUri + "&response_type=code&scope=likes+comments+relationships";
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        HttpResponse response = client.execute(request);
        BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        System.out.println(result.toString());

    }
    catch(Exception e)
    {
        System.out.print("GET CODE ERROR->"+e.toString());
    }

}

【问题讨论】:

  • 仔细检查一下,您是否已在 instagram 上注册为开发人员? instagram.com/developer/register
  • 是的,当然。也忘了提一下,我曾经得到一个代码,我给了所有权限。所以现在我提出请求,我用代码直接重定向到 URL。
  • 你能分享一下结果吗?我也有同样的情况,我找不到办法。

标签: java oauth instagram


【解决方案1】:

REDIRECT_URI 更改为http://localhost。所以实际的“重定向”代码应该在你身边完成。您可以解析参数code 的值。另请查看this question 的答案。这将帮助您使用代码获取 URI。

【讨论】:

  • 您需要一台服务器吗?
  • 没有。 HttpURLConnection#getResponseCode() 和来自URLConnection#getHeaderField()Location 标头就足够了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
  • 2019-04-14
  • 2014-11-04
  • 2016-10-15
  • 2020-12-11
  • 2018-11-21
相关资源
最近更新 更多