Google api Google OAuth 2正在生成重定向uri,而不是使用client_secret.json中定义的uri

Google api Google OAuth 2正在生成重定向uri,而不是使用client_secret.json中定义的uri,google-api,oauth-2.0,google-oauth,Google Api,Oauth 2.0,Google Oauth,我想使用谷歌日历API,但为了做到这一点,我需要获得使用谷歌OAuth 2.0 API的授权。我遇到了重定向uri的问题。以下是我的客户机_secret.json的示例 { "web": { "client_id": "deleted", "client_secret": "deleted", "redirect_uris": ["http://localhost:8080/CommunityUmcPasadena/Callback"], "auth

我想使用谷歌日历API,但为了做到这一点,我需要获得使用谷歌OAuth 2.0 API的授权。我遇到了重定向uri的问题。以下是我的客户机_secret.json的示例

{
   "web": {
     "client_id": "deleted",
     "client_secret": "deleted",
     "redirect_uris": ["http://localhost:8080/CommunityUmcPasadena/Callback"],
     "auth_uri": "https://accounts.google.com/o/oauth2/auth",
     "token_uri": "https://accounts.google.com/o/oauth2/token"
   }
}
运行Quickstart应用程序时,出现以下错误:

Apr 20, 2017 10:45:42 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNING: unable to change permissions for everybody: C:\Users\Gary\.credentials\calendar-java-quickstart
Apr 20, 2017 10:45:42 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNING: unable to change permissions for owner: C:\Users\Gary\.credentials\calendar-java-quickstart
2017-04-20 22:45:42.485:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2017-04-20 22:45:42.485:INFO::jetty-6.1.26
2017-04-20 22:45:42.498:INFO::Started SocketConnector@localhost:34940
Please open the following address in your browser:
  https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=deleted&redirect_uri=http://localhost:34940/Callback&response_type=code&scope=https://www.googleapis.com/auth/calendar.readonly
Attempting to open that address in the default browser now...
如您所见,它的重定向uri为。但这不是client_secret.json中定义的。但它使用的是正确的客户端id和密码。因此,我不确定api为什么会生成随机回调。我还要注意,client_secret.json中列出的重定向_uri与API管理器相同

有人知道为什么重定向uri是由API生成的,而不是使用client_secret.json中定义的uri吗

非常感谢您的帮助

另外,这里是快速入门应用程序的代码

package sample;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.client.util.DateTime;

import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class Quickstart {
    private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart";

    private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".credentials/calendar-java-quickstart");
    private static FileDataStoreFactory DATA_STORE_FACTORY;
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    private static HttpTransport HTTP_TRANSPORT;

    private static final List<String> SCOPES = Arrays.asList(CalendarScopes.CALENDAR_READONLY);

    static {
        try {
            HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
        }
        catch(Throwable t) {
            t.printStackTrace();
            System.exit(1);
        }
    }

    public static Credential authorize() throws IOException {
        InputStream in = Quickstart.class.getResourceAsStream("/client_secret.json");

        GoogleClientSecrets clientSecrets = 
                GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();

        Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");

        System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());

        return credential;
    }

    public static com.google.api.services.calendar.Calendar getCalendarService() throws IOException {
        Credential credential = authorize();

        return new com.google.api.services.calendar.Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();

    }

    public static void main(String[] args) throws IOException {
        com.google.api.services.calendar.Calendar service = getCalendarService();


    }
}
包装样品;
导入com.google.api.client.auth.oauth2.Credential;
导入com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
导入com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
导入com.google.api.client.googleapis.auth.oauth2.googleaauthorizationcodeflow;
导入com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
导入com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
导入com.google.api.client.http.HttpTransport;
导入com.google.api.client.json.jackson2.JacksonFactory;
导入com.google.api.client.json.JsonFactory;
导入com.google.api.client.util.store.FileDataStoreFactory;
导入com.google.api.client.util.DateTime;
导入com.google.api.services.calendar.CalendarScopes;
导入com.google.api.services.calendar.model.*;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.array;
导入java.util.List;
公共类快速入门{
私有静态最终字符串应用程序\u NAME=“Google日历API Java Quickstart”;
private static final java.io.File DATA_STORE_DIR=new java.io.File(System.getProperty(“user.home”),“.credentials/calendar java quickstart”);
私有静态文件数据存储工厂数据存储工厂;
私有静态最终JsonFactory JSON_FACTORY=JacksonFactory.getDefaultInstance();
专用静态HttpTransport HTTP_传输;
私有静态最终列表范围=Arrays.asList(CalendarScopes.CALENDAR\u只读);
静止的{
试一试{
HTTP_TRANSPORT=GoogleNetHttpTransport.newTrustedTransport();
DATA\u STORE\u FACTORY=新文件datastorefactory(DATA\u STORE\u DIR);
}
捕获(可丢弃的t){
t、 printStackTrace();
系统出口(1);
}
}
公共静态凭据授权()引发IOException{
InputStream in=Quickstart.class.getResourceAsStream(“/client_secret.json”);
谷歌客户机密clientSecrets=
load(JSON_工厂,新的InputStreamReader(in));
GoogleAuthorizationCodeFlow=新的GoogleAuthorizationCodeFlow.Builder(HTTP_传输、JSON_工厂、clientSecrets、作用域)
.setDataStoreFactory(数据存储工厂).setAccessType(“脱机”).build();
Credential Credential=new AuthorizationCodeInstalledApp(流,new LocalServerReceiver())。授权(“用户”);
System.out.println(“凭证保存到”+DATA\u STORE\u DIR.getAbsolutePath());
返回凭证;
}
public static com.google.api.services.calendar.calendar getCalendarService()引发IOException{
凭证=授权();
返回新的com.google.api.services.calendar.calendar.Builder(HTTP_传输、JSON_工厂、凭证)
.setApplicationName(应用程序名称)
.build();
}
公共静态void main(字符串[]args)引发IOException{
com.google.api.services.calendar.calendar服务=getCalendarService();
}
}

您的代码没有指定重定向url。我怀疑Java库是在假设,如果您没有指定重定向url,那是因为您没有重定向url,所以它默认为假url。看起来您已经复制/粘贴了Quickstart代码,该代码在页面顶部显示“一个简单的Java命令行应用程序”,而我认为您正在构建一个web服务器应用程序


所以,深入研究JavaOAuth库文档(祝你好运-试试),看看在哪里设置你的
ttp://localhost:8080/CommunityUmcPasadena/Callback

我已经寻找答案很久了

你需要使用

用法示例:

 LocalServerReceiver localServerReceiver = new LocalServerReceiver.Builder().setHost("localhost").setPort(8181).build();
 Credential credential = new AuthorizationCodeInstalledApp(flow, localServerReceiver).authorize("user");

可能有关联?是的,我已经开始这么做了。谢谢