首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取accessToken

获取accessToken
EN

Stack Overflow用户
提问于 2013-10-29 09:53:44
回答 1查看 430关注 0票数 0

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

代码语言:javascript
复制
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获取令牌。

//编辑:

代码语言:javascript
复制
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文件。代码是这样的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-29 10:09:57

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

代码语言:javascript
复制
        URL newURL = new URL(GraphURL1);
        HttpsURLConnection https = (HttpsURLConnection) newURL.openConnection();
        https.setRequestMethod("HEAD");
        https.setUseCaches(false);
        String query = newURL.getQuery();

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

代码语言:javascript
复制
Map<String, String> queryMap = getQueryMap(query );

获取查询地图的方法

代码语言:javascript
复制
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;  
}

下面的方法将您的令牌写入文件中,只需传递令牌即可

代码语言:javascript
复制
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();
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19654811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档