2017-04-21 45 views
0

我想使用Google Calendar API,但爲了這樣做,我需要使用Google Googles OAuth 2.0 API進行授權。我遇到了redirect_uri的麻煩。以下是我的client_secret.json的示例。Google OAuth 2正在生成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" 
    } 
} 

當我跑我的快速啓動應用程序時,我得到以下錯誤:

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 [email protected]: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相同。

有誰知道爲什麼redirect_uri是由API生成的,而不是使用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(); 


    } 
} 
+0

也許有關係嗎? https://stackoverflow.com/questions/22595174/google-oauth-the-redirect-uri-in-the-request-did-not-match-a-registered-redirec – Alex

回答

1

您的代碼不指定REDIRECT_URL。我懷疑Java庫做出的假設是,如果你沒有指定redirect_url,那是因爲你沒有一個,所以它默認爲一個假的URL。看起來你已經複製/粘貼了Quickstart代碼,該代碼在頁面頂部顯示「一個簡單的Java 命令行應用程序」,而我認爲你正在構建一個Web服務器應用程序。

的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),看看那裏的重定向URL設置爲您ttp://localhost:8080/CommunityUmcPasadena/Callback

+0

是的,我已經開始這樣做了。謝謝 –

相關問題