从谷歌访问个人资料数据加上获得以下异常时我的代码如下

时间:2016-08-25 13:51:39

标签: google-api

  

线程中的异常" main"   com.google.api.client.googleapis.json.GoogleJsonResponseException:404   找不到找不到   com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)     在   com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)     在   com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)     在   com.google.api.client.googleapis.services.AbstractGoogleClientRequest $ 1.interceptResponse(AbstractGoogleClientRequest.java:312)     在   com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1045)     在   com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)     在   com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)     在   com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)     在googleplusdemo2.MyClass.main(MyClass.java:107)

public class MyClass {

// List the scopes your app requires:
private static List<String> SCOPE = Arrays.asList(
    "https://www.googleapis.com/auth/plus.me",
    "https://www.googleapis.com/auth/plus.profiles.read",
    "https://www.googleapis.com/auth/plus.circles.write",
    "https://www.googleapis.com/auth/plus.stream.write",
    "https://www.googleapis.com/auth/plus.stream.read");

// The following redirect URI causes Google to return a code to the user's
// browser that they then manually provide to your app to complete the
// OAuth flow.
private static final String REDIRECT_URI = "http://some url";
static String CLIENT_ID="myclient id";
static String CLIENT_SECRET="myclient secret";
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            new NetHttpTransport(),
            new JacksonFactory(),
            CLIENT_ID, // This comes from your Developers Console project
            CLIENT_SECRET, // This, as well
            SCOPE)
            .setApprovalPrompt("force")
            .setAccessType("offline").build();


        String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
        System.out.println("Please open the following URL in your browser then " +
            "type the authorization code:");
        System.out.println("  " + url);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String code = br.readLine();
        // End of command line prompt for the authorization code.

        GoogleTokenResponse tokenResponse = flow.newTokenRequest(code)
            .setRedirectUri(REDIRECT_URI).execute();
        GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(new NetHttpTransport())
            .setJsonFactory(new JacksonFactory())
            .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
            .addRefreshListener(new CredentialRefreshListener() {
              @Override
              public void onTokenResponse(Credential credential, TokenResponse tokenResponse) {
                // Handle success.
                System.out.println("Credential was refreshed successfully.");
              }

              @Override
              public void onTokenErrorResponse(Credential credential,
                  TokenErrorResponse tokenErrorResponse) {
                // Handle error.
                System.err.println("Credential was not refreshed successfully. "
                    + "Redirect to error page or login screen.");
              }
            })
            .build();

        // Set authorized credentials.
        credential.setFromTokenResponse(tokenResponse);

        credential.refreshToken();

        // Create a new authorized API client

        PlusDomains plusDomains = new PlusDomains.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName("TESTMK3").build();

        Person mePerson = plusDomains.people().get("me").execute();
        mePerson.getGender();
        System.out.println("ID:\t" + mePerson.getId());
        System.out.println("Display Name:\t" + mePerson.getDisplayName());
        System.out.println("Image URL:\t" + mePerson.getImage().getUrl());
        System.out.println("Profile URL:\t" + mePerson.getUrl());


}

}

0 个答案:

没有答案