- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现此阶段的项目 - https://hyperskill.org/projects/62/stages/337/implement 。我需要将 String code = Exchange.getRequestURI().getQuery();
的结果存储在变量中,并在 POST 请求中使用它。
POST(HttpRequest.BodyPublishers.ofString("client_id="+ CLIENT_ID + "&client_secret="+ CLIENT_SECRET+ "&grant_type="+ GRANT_TYPE + "&code="+ 代码 + "&redirect_uri="+ REDIRECT_URI))
但我不能这样做,因为它不存储在 server.createContext 之外的可用空间中。也许我想做错事?谁能帮我吗?
private static final String CLIENT_ID = "da072c60fcee469e8b0f4140aa4480d5";
private static final String CLIENT_SECRET = "8ada13093c704487b57c3a660448884e";
private static final String AUTHORIZE_ADDRESS = "https://accounts.spotify.com/authorize";
private static final String RESPONSE_TYPE = "code";
private static final String TOKEN_ADDRESS = "https://accounts.spotify.com/api/token";
private static final String GRANT_TYPE = "authorization_code";
private static final String CODE = "";
private static final String REDIRECT_URI = "http://localhost:8080";
private static final String ANSWER_DENIED_ACCESS = "Please, provide access for application.";
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
boolean successfulAccess = false;
while (sc.hasNext()) {
String input = sc.next();
switch (input) {
case "auth":
server();
request();
successfulAccess = true;
System.out.println("---SUCCESS---");
break;
}
}
}
private static void server() throws IOException {
HttpServer server = HttpServer.create();
server.bind(new InetSocketAddress(8080), 0);
server.start();
System.out.println("use this link to request the access code:");
System.out.println(AUTHORIZE_ADDRESS
+ "?client_id=" + CLIENT_ID
+ "&redirect_uri=" + REDIRECT_URI
+ "&response_type=" + RESPONSE_TYPE);
System.out.println("waiting for code...");
server.createContext("/",
exchange -> {
String code = exchange.getRequestURI().getQuery();
String result = "";
String answer = "";
if (code.contains("code")) {
result = "Got the code. Return back to your program.";
answer = "code received";
} else {
result = "Not found authorization code. Try again.";
answer = "code didn't received";
}
exchange.sendResponseHeaders(200, result.length());
exchange.getResponseBody().write(result.getBytes());
exchange.getResponseBody().close();
System.out.println(answer);
}
);
server.stop(10);
}
private static void request() throws IOException, InterruptedException {
System.out.println("making http request for access_token...");
HttpRequest request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(
"client_id=" + CLIENT_ID
+ "&client_secret=" + CLIENT_SECRET
+ "&grant_type=" + GRANT_TYPE
+ "&code=" + CODE
+ "&redirect_uri=" + REDIRECT_URI))
.header("Content-Type", "application/x-www-form-urlencoded")
.uri(URI.create(TOKEN_ADDRESS))
.build();
HttpClient client = HttpClient.newBuilder().build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("response:");
System.out.println(response.body());
}
}```
最佳答案
我需要使用 Thread.sleep 进行 while 循环,而不是通常可以将代码存储在变量中。像这样:
exchange -> {
String code = exchange.getRequestURI().getQuery();
String result = "";
String answer = "";
if (code != null && code.contains("code")) {
CODE = code.substring(5);
result = "Got the code. Return back to your program.";
answer = "code received";
} else {
result = "Not found authorization code. Try again.";
answer = "code not received";
}
exchange.sendResponseHeaders(200, result.length());
exchange.getResponseBody().write(result.getBytes());
exchange.getResponseBody().close();
System.out.println(answer);
}
);
while (CODE.equals("")) {
Thread.sleep(10);
}
server.stop(10);```
关于java - 如何将 Exchange.getRequestURI().getQuery() 的结果存储在变量中并使用它来获取授权码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61265292/
尝试在 Laravel 中处理数据表: 错误: 在 Laravel 数据表中的数组上调用成员函数 getQuery() 这是 Laravel 代码: Controller : public funct
Documentum 示例代码从未被深入注释过,所以我的问题是: 这行是什么意思? IDfQuery query = DfcUtils.getClientX().getQuery(); 最佳答案 您可
当我执行方法 getMachineSettings 时出现错误: Fatal error: Uncaught Error: Call to undefined method Doctrine\DBAL
在我的测试项目中,我有 2 个实体: - 最终用户(FOSUserBundle 的扩展) - Rezo(将包含两个成员之间的批准关系) 这两个实体都被定义为: 最终用户实体: disciplines
我正在使用ongr / elasticsearch-dsl bundle 包构建查询,并且需要在不同位置组成搜索对象。当我在同一位置进行操作时,它可以按预期工作。 use ONGR\Elasticse
/search.aspx?Search=测试 function getQuery(name) { var match = RegExp('[?&]' + name + '=([^&]*)').
这个问题已经有答案了: Dynamic "string" in R (4 个回答) 已关闭 5 年前。 是否可以将值传递到 dbGetQuery 中的查询中来自 RMySQL 包。 例如,如果我在字符
大家下午好!我正在 Android Studio 中开发一个搜索系统,但我是 Java 语言的初学者,我的错误可能很愚蠢。我想帮助理解两件事。第一:为什么我不能访问 this.getApplicati
我认为使用 getQuery丢失信息,很危险,而只有 getRawQuery应该使用,并且任何已知编码的查询参数值都应该用 URLDecoder 手动解码(一旦原始查询在 & 字符上拆分) . 恰当的
我正在尝试实现此阶段的项目 - https://hyperskill.org/projects/62/stages/337/implement 。我需要将 String code = Exchange
我正在实现一个包含搜索服务(AbstractService)的osgi包。该服务包含一个以 NodeIterator 对象的形式获取内容节点列表的方法。 public NodeIterator ge
我在 Laravel 上工作了很多,从来没有遇到过以下错误: 在 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.
在 ParseQueryAdapter 中,我想返回我正在查询的对象的关系。这是我目前所拥有的;我正在执行查询,检索当前用户创建的所有目标;在 public View getItemView 中,我开
方法一 global $database; $user = new stdClass; $user->id = NULL; $user->name = $name; $user->
我想使用 joomla 内置数据库类构建以下查询。 SELECT * FROM table_name ORDER BY id DESC LIMIT 1 这是我到目前为止建立的查询。 $db =& JF
我正在学习请求类。 我有这个网址http://localahost/blog/search?year=2013&month=07 当我尝试 print_r($_GET) 时,它会按预期返回数组。 当我
我正在学习请求类。 我有这个网址 http://localahost/blog/search?year=2013&month=07 当我尝试 print_r($_GET) 时,它会按预期返回数组。 当
我有以下简单的 Controller : class OrdersController extends \BaseController { public function index()
我正在编写一个半包装 Java 的 URL 类 的实用程序类,并且我已经编写了一堆测试用例来验证我用自定义实现包装的方法。我不明白某些 Java 的某些 URL 字符串的 getter 的输出。 根据
我是一名优秀的程序员,十分优秀!