gpt4 book ai didi

java - 获取accessToken

转载 作者:行者123 更新时间:2023-12-01 13:55:33 24 4
gpt4 key购买 nike

如果我使用链接,我怎样才能获得我的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中获取 token 。

//编辑:

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 文件。代码是这样的enter image description here :

最佳答案

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

        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 );

获取查询Map的方法

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;
}

以下方法会将您的 token 写入文件,您只需传递 token 即可

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();
}
}

关于java - 获取accessToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19654811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com