1

我正在尝试使用以下代码在我的 Android 应用程序中使用 Google 联系人 API 访问我的 Google 帐户联系人。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        ContactsService myService = new ContactsService("GoogleContactsProject");


        GoogleOAuthParameters oauthParam = new GoogleOAuthParameters();
        oauthParam.setOAuthConsumerKey("9****************u9473opkpetc*****v3.apps.googleusercontent.com");
        oauthParam.setOAuthConsumerSecret("A*****************415yy6vAipya4");
        oauthParam.setOAuthType(OAuthParameters.OAuthType.TWO_LEGGED_OAUTH);

        myService.setOAuthCredentials(oauthParam, new OAuthHmacSha1Signer());

        (new HelloThread(myService)).start();


    } catch (Exception e) {
        e.printStackTrace();
    }

}

public class HelloThread extends Thread {
    ContactsService myServ;

    HelloThread(ContactsService myService) {
        this.myServ = myService;
    }

    public void run() {
        System.out.println("Hello from a thread!");

        try {
            printAllContacts(myServ);


        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

public static void printAllContacts(ContactsService myService)
        throws ServiceException, IOException {


    // Request the feed
    URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
    ContactFeed resultFeed = myService.getFeed(feedUrl, ContactFeed.class);
    // Print the results
    System.out.println(resultFeed.getTitle().getPlainText());
    for (ContactEntry entry : resultFeed.getEntries()) {
        if (entry.hasName()) {
            Name name = entry.getName();
            if (name.hasFullName()) {
                String fullNameToDisplay = name.getFullName().getValue();
                if (name.getFullName().hasYomi()) {
                    fullNameToDisplay += " (" + name.getFullName().getYomi() + ")";
                }
                System.out.println("\t\t" + fullNameToDisplay);
            } else {
                System.out.println("\t\t (no full name found)");
            }
            if (name.hasNamePrefix()) {
                System.out.println("\t\t" + name.getNamePrefix().getValue());
            } else {
                System.out.println("\t\t (no name prefix found)");
            }
            if (name.hasGivenName()) {
                String givenNameToDisplay = name.getGivenName().getValue();
                if (name.getGivenName().hasYomi()) {
                    givenNameToDisplay += " (" + name.getGivenName().getYomi() + ")";
                }
                System.out.println("\t\t" + givenNameToDisplay);
            } else {
                System.out.println("\t\t (no given name found)");
            }
            if (name.hasAdditionalName()) {
                String additionalNameToDisplay = name.getAdditionalName().getValue();
                if (name.getAdditionalName().hasYomi()) {
                    additionalNameToDisplay += " (" + name.getAdditionalName().getYomi() + ")";
                }
                System.out.println("\t\t" + additionalNameToDisplay);
            } else {
                System.out.println("\t\t (no additional name found)");
            }
            if (name.hasFamilyName()) {
                String familyNameToDisplay = name.getFamilyName().getValue();
                if (name.getFamilyName().hasYomi()) {
                    familyNameToDisplay += " (" + name.getFamilyName().getYomi() + ")";
                }
                System.out.println("\t\t" + familyNameToDisplay);
            } else {
                System.out.println("\t\t (no family name found)");
            }
            if (name.hasNameSuffix()) {
                System.out.println("\t\t" + name.getNameSuffix().getValue());
            } else {
                System.out.println("\t\t (no name suffix found)");
            }
        } else {
            System.out.println("\t (no name found)");
        }
        System.out.println("Email addresses:");
        for (Email email : entry.getEmailAddresses()) {
            System.out.print(" " + email.getAddress());
            if (email.getRel() != null) {
                System.out.print(" rel:" + email.getRel());
            }
            if (email.getLabel() != null) {
                System.out.print(" label:" + email.getLabel());
            }
            if (email.getPrimary()) {
                System.out.print(" (primary) ");
            }
            System.out.print("\n");
        }
        System.out.println("IM addresses:");
        for (Im im : entry.getImAddresses()) {
            System.out.print(" " + im.getAddress());
            if (im.getLabel() != null) {
                System.out.print(" label:" + im.getLabel());
            }
            if (im.getRel() != null) {
                System.out.print(" rel:" + im.getRel());
            }
            if (im.getProtocol() != null) {
                System.out.print(" protocol:" + im.getProtocol());
            }
            if (im.getPrimary()) {
                System.out.print(" (primary) ");
            }
            System.out.print("\n");
        }
        System.out.println("Groups:");
        for (GroupMembershipInfo group : entry.getGroupMembershipInfos()) {
            String groupHref = group.getHref();
            System.out.println("  Id: " + groupHref);
        }
        System.out.println("Extended Properties:");
        for (ExtendedProperty property : entry.getExtendedProperties()) {
            if (property.getValue() != null) {
                System.out.println("  " + property.getName() + "(value) = " +
                        property.getValue());
            } else if (property.getXmlBlob() != null) {
                System.out.println("  " + property.getName() + "(xmlBlob)= " +
                        property.getXmlBlob().getBlob());
            }
        }
        Link photoLink = entry.getContactPhotoLink();
        String photoLinkHref = photoLink.getHref();
        System.out.println("Photo Link: " + photoLinkHref);
        if (photoLink.getEtag() != null) {
            System.out.println("Contact Photo's ETag: " + photoLink.getEtag());
        }
        System.out.println("Contact's ETag: " + entry.getEtag());
    }
}

但我没有得到像这样的错误

进程:com.itconquest.myapplication,PID:3697 java.lang.NullPointerException:在 com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608) 在 com.google.gdata.client 没有身份验证标头信息.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564) 在 com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560) 在 com.google.gdata.client.http.HttpGDataRequest。在 com.google.gdata.client.Service.getFeed(Service.java:1135) 在 com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536) 处执行(HttpGDataRequest.java:538) .google.gdata.client.Service.getFeed(Service.java:998) 在 com.google.gdata.client.GoogleService.getFeed(GoogleService.java:645) 在 com.google.gdata.client.Service.getFeed(Service .java:1017) 在 com.itconquest.myapplication.MainActivity.printAllContacts(MainActivity.java:82) 在 com.itconquest.myapplication.MainActivity$HelloThread.run(MainActivity.java:60) 09-05 12:28:45.194 3697-3772/ com.itconquest.myapplication E/Surface:getSlotFromBufferLocked:未知缓冲区:0xae3f3b50

这是我的 Gradle 文件

apply plugin: 'com.android.application'

android { compileSdkVersion 24 buildToolsVersion "24.0.2"

defaultConfig {
    applicationId "com.itconquest.myapplication"
    minSdkVersion 15
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

} 存储库 { mavenCentral() } buildscript { 存储库 { maven { url ' http://repo1.maven.org/maven2 ' } }

} 依赖项 { 编译 fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.2.0' compile 'com. google.api-client:google-api-client:1.22.0' 编译 'com.google.gdata:core:1.47.1' }

我的谷歌账户截图如下

注意:我使用 OAuth 2.0 客户端 ID 作为 setOAuthConsumerKey,使用 API 密钥作为 setOAuthConsumerSecret

在此处输入图像描述

如何解决这个问题?

4

0 回答 0