【问题标题】:Getting accessToken获取 accessToken
【发布时间】:2013-11-08 09:45:44
【问题描述】:

如果我使用链接,我如何获得我的 accessToken:

https://www.facebook.com/login.php?skip_api_login=1&api_key=MY_APP_TOKEN&signed_next=1&next=https://www.facebook.com/dialog/oauth?redirect_uri=http%253A%252F%252Fwww.facebook.com%252Fconnect%252Flogin_success.html&scope=read_stream%252Coffline_access&type=user_agent&client_id=389735501155841&ret=login&cancel_uri=http://www.facebook.com/connect/login_success.html?error=access_denied&error_code=200&error_description=Permissions%2berror&error_reason=user_denied#_=_&display=page

我想在 Java 中获取令牌。

//编辑:

String GraphURL1 = "https://www.facebook.com/dialog/oauth?client_id=APPTOKEN&redirect_uri=https%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html&response_type=token&display=popup&scope=user_about_me%2Cread_stream%2C%20share_item";
            URL newURL = new URL(GraphURL1);
            HttpsURLConnection https = (HttpsURLConnection)newURL.openConnection();
            https.setRequestMethod("HEAD");
            https.setUseCaches(false);

//编辑:我已经保存了 token.txt 文件。代码是这样的:

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我使用了另一个redirect_uri,但这不是我想要的。字符串 GraphURL1 = "facebook.com/dialog/…"; URL newURL = 新 URL(GraphURL1); HttpsURLConnection https = (HttpsURLConnection)newURL.openConnection(); https.setRequestMethod("头"); https.setUseCaches(false);

标签: java token facebook-login access-token facebook-access-token


【解决方案1】:

使用以下代码。它将返回所有查询参数的映射

        URL newURL = new URL(GraphURL1);
        HttpsURLConnection https = (HttpsURLConnection) newURL.openConnection();
        https.setRequestMethod("HEAD");
        https.setUseCaches(false);
        String query = newURL.getQuery();

使用以下代码。它将返回所有查询参数的映射

Map<String, String> queryMap = getQueryMap(query );

查询地图获取方法

public static Map<String, String> getQueryMap(String query )  
{  

    String[] params = query.split("&");  
    Map<String, String> map = new HashMap<String, String>();  
    for (String param : params)  
    {  
        String name = param.split("=")[0];  
        String value = param.split("=")[1];  
        map.put(name, value);  
    }  
    return map;  
}

以下方法会将您的令牌写入文件,您只需传递令牌

public void writeTokenIntoFile(String token)
    {   
        try{


            File file =new File("c://token.txt");

            //if file doesnt exists, then create it
            if(!file.exists()){
                file.createNewFile();
            }

            //true = append file
            FileWriter fileWritter = new FileWriter(file.getName(),true);
                BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
                bufferWritter.write(token);
                bufferWritter.close();

            System.out.println("Done");

        }catch(IOException e){
            e.printStackTrace();
        }
    }

【讨论】:

  • 我有一个问题: Map queryMap = getQueryMap(query ); ->查询是红色下划线:/
  • 用这个来查询String query = newURL.getQuery();
  • 效果很好。谢谢。但是如何将我的 accessToken 保存在 txt 文件中进行测试? PrintWriter spWriter = new PrintWriter(new FileWriter("accessToken.txt")); spWriter.println(查询); spWriter.flush();文件file = new File("accessToken.txt");
  • 我已经测试了你的代码,但我只得到了完整的 html 代码。如何在文本文件中获取令牌....?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
相关资源
最近更新 更多