我一直在为我在 weebly 创建的网站使用 Google Analytics 。我正在使用 Google API 以编程方式实现整个过程。
我在 OAuth 流程中面临的问题是我收到了这个错误:
Error: redirect_uri_mismatch
The redirect URI in the request:localhost:34190/Callback did not match a registered redirect URI
Request Details
scope=https://www.googleapis.com/auth/analytics.readonly
response_type=code
access_type=online
redirect_uri=local_host:34190/Callback
display=page
client_id={CLIENT_ID}.apps.googleusercontent.com
我的谷歌 api 控制台配置是:
Redirect URI: localhost/oauth2callback
JavaScript origins: localhost
为什么我将它设置为redirect_uri
:?localhost:34190/Callback
http://mya.com/oauth2callback
我为 openauth 编写的代码:
public static void main(String[] args) throws Exception {
Analytics analytics = initializeAnalytics();
}
private static Analytics initializeAnalytics() throws Exception {
Credential credential = authorize();
}
private static Credential authorize() throws Exception {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
JSON_FACTORY, Testcode.class.getResourceAsStream("/client_secrets.json"));
FileCredentialStore credentialStore = new FileCredentialStore(
new File(System.getProperty("user.home"), ".credentials/analytics.json"),
JSON_FACTORY);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets,
Collections.singleton(AnalyticsScopes.ANALYTICS_READONLY)).setCredentialStore(
credentialStore).build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
.authorize(clientSecrets.getDetails().getClientId());
}
我怎样才能摆脱这个错误?