我正在使用Google OAuth2代表服务帐户与Google DialogFlow API进行服务器到服务器的交互。 但是当尝试生成访问令牌(credentials.refreshIfExpired())时出现错误:
java.io.IOException: Error getting access token for service account: connect timed out
此处是代码:
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(System.getenv("GOOGLE_APPLICATION_CREDENTIALS")));
if (credentials.createScopedRequired()) {
credentials = credentials.createScoped(Collections.singletonList("https://www.googleapis.com/auth/dialogflow"));
}
credentials.refreshIfExpired();
AccessToken token = credentials.getAccessToken();
System.out.println(token.getTokenValue());
我试图通过多种方式找到解决方案,有人说这可能导致我的服务器时间与Google时间之间的同步。
然后我在格林尼治标准时间设置默认timeZone
@SpringBootApplication
public class DialogFlowDemoApplication {
@PostConstruct
public void init(){
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
System.out.println("Spring boot application running in GMT timezone :"+new Date());
}
public static void main(String[] args) {
SpringApplication.run(DialogFlowDemoApplication.class, args);
}
}
但这没有帮助,这真的让我发疯。
我的依赖关系如下:
<!--start google dependencies-->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-dialogflow</artifactId>
<version>0.108.0-alpha</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.30.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.90.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.30.4</version>
</dependency>
<!--end of google dependencies-->
请帮助,谢谢!