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

时间:2017-04-21 02:58:33

标签: google-api oauth-2.0 google-oauth

我想使用Google Calendar API,但为了做到这一点,我需要使用Googles OAuth 2.0 API进行授权。我遇到了redirect_uri的问题。以下是我的client_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...

如您所见,它的redirect_uri为http://localhost:34940/Callback。这不是client_secret.json中定义的内容。它使用正确的client_id和秘密。因此,我不确定为什么api会产生随机回调。我还要注意,client_secret.json中列出的redirect_uri与API Manager相同。

有谁知道为什么API会生成redirect_uri而不是使用client_secret.json中定义的那个?

非常感谢任何帮助。

此外,这是快速启动应用程序的代码......

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


    }
}

2 个答案:

答案 0 :(得分:1)

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

Sooo,深入了解Java OAuth库文档(祝你好运 - 试试https://developers.google.com/api-client-library/java/google-oauth-java-client/reference/1.20.0/com/google/api/client/auth/oauth2/AuthorizationCodeFlow),看看将重定向网址设置到ttp://localhost:8080/CommunityUmcPasadena/Callback

的位置

答案 1 :(得分:1)

我一直在寻找答案...

您需要使用 LocalServerReceiver

用法示例:

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