2017-01-13 8 views
1

stackoverflow에서 몇 가지 질문이 있지만 내 문제를 해결하기위한 답변이없는 것으로 알고 있습니다. Google PHP Api 클라이언트 라이브러리 v2.0.1을 사용 중이며 애플리케이션이 서버 간 애플리케이션입니다. 정확하게 Google Contacts API V3 in PHP를 사용하여 연락처/그룹을 만들 수 없습니다.

  • 아래 코드 작성을위한 연락처 그룹
  • 만들기 연락처 만들기

    1. : 내 응용 프로그램은 내가 연락처/그룹을 나열 할 수있는 API의 예에서 읽을 수 있지만 내가 할 수 없습니다입니다 접촉 : 다음과 같은 응답을 반환

      putenv('GOOGLE_APPLICATION_CREDENTIALS='.APP_PATH.'access_token.json'); 
      
      define('CREDENTIALS_PATH',APP_PATH .'access_token.json'); 
      define('CLIENT_SECRET_PATH', APP_PATH. 'client_secret.json'); 
      
      define('SCOPES', implode(' ', [ 
           'https://www.google.com/m8/feeds' 
          ] 
      )); 
      
      $client = new Google_Client(); 
      $client->setApplicationName('My Contacts App'); 
      $client->setDeveloperKey($devKey); 
      $client->setSubject("[email protected]"); 
      $client->setAccessType('offline'); 
      $client->setIncludeGrantedScopes(true); 
      $client->useApplicationDefaultCredentials(); 
      $client->addScope(SCOPES); 
      
      // Get Guzzle Client 
      $httpClient = $client->authorize(); 
      
      $xml = '<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:gd="http://schemas.google.com/g/2005"> 
      <atom:category scheme="http://schemas.google.com/g/2005#kind" 
      term="http://schemas.google.com/contact/2008#contact"/> 
      <gd:name> 
      <gd:givenName>Elizabeth</gd:givenName> 
      <gd:familyName>Bennet</gd:familyName> 
      <gd:fullName>Elizabeth Bennet</gd:fullName> 
      </gd:name> 
      <atom:content type="text">Notes</atom:content> 
      <gd:email rel="http://schemas.google.com/g/2005#work" 
      primary="true" 
      address="[email protected]" displayName="E. Bennet"/> 
      <gd:email rel="http://schemas.google.com/g/2005#home" 
      address="[email protected]"/> 
      </atom:entry>'; 
      
      $headers = [ 
          'Content-Type' => 'application/atom+xml' 
      ]; 
      
      $response = $httpClient->post(
          'https://www.google.com/m8/feeds/contacts/default/full', 
          $headers, 
          $xml 
      ); 
      
      echo $response->getBody(); 
      

      :

      <?xml version="1.0" encoding="UTF-8"?> 
      <entry xmlns="http://www.w3.org/2005/Atom" xlns:batch="http://schemas.google.com/gdata/batch" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:gd="http://schemas.google.com/g/2005"> 
      <id>http://www.google.com/m8/feeds/contacts/[email protected]/base/fe770968a626294</id> 
      <updated>2017-01-13T08:45:06.790Z</updated> 
      <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/> 
      <title type="text"/> 
      <link rel="http://schemas.google.com/contacts/2008/rel#edit-photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/[email protected]/fe770968a626294/1B2M2Y8AsgTpgAmY7PhCfg"/> 
      <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/[email protected]/full/fe770968a626294"/> 
      <link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/[email protected]/full/fe770968a626294/1484297106790001"/> 
      </entry> 
      

      이 경우 속성을 설정하지 않고 빈 연락처를 만들게됩니다. 아무도 내가 잘못 가고있는 것을 볼 수 있습니까? 어떤 도움이라도 대단히 감사합니다.

      솔루션

      내가 잘못 목구멍 요청을 수행 한 - 코드의 마지막 목구멍 부분은 다음과 같이 봤어야 관심이 누군가를 위해 RTM

      이 있어야합니다

      $request = new \GuzzleHttp\Psr7\Request(
          'POST', 
          'https://www.google.com/m8/feeds/contacts/default/full', 
          ['Content-Type' => 'application/atom+xml; charset=UTF-8; type=entry'], 
          $xml 
      ); 
      
      $response = $httpClient->send($request); 
      
    +0

    답변을 솔루션으로 게시해야합니다. – KENdi

    +0

    감사합니다. @KENdi 답변으로 해결책을 게시했습니다. – amburnside

    답변

    1

    서버 대 서버 응용 프로그램에서 연락처를 만드는 데 필요한 완벽한 솔루션을 원한다면 다음과 같습니다.

    putenv('GOOGLE_APPLICATION_CREDENTIALS='.APP_PATH.'access_token.json'); 
    
    define('CREDENTIALS_PATH',APP_PATH .'access_token.json'); 
    define('CLIENT_SECRET_PATH', APP_PATH. 'client_secret.json'); 
    
    define('SCOPES', implode(' ', [ 
         'https://www.google.com/m8/feeds' 
        ] 
    )); 
    
    $client = new Google_Client(); 
    $client->setApplicationName('My Contacts App'); 
    $client->setDeveloperKey($devKey); 
    $client->setSubject("[email protected]"); 
    $client->setAccessType('offline'); 
    $client->setIncludeGrantedScopes(true); 
    $client->useApplicationDefaultCredentials(); 
    $client->addScope(SCOPES); 
    
    // Get Guzzle Client 
    $httpClient = $client->authorize(); 
    
    $xml = '<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005"> 
    <atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/> 
    <gd:name> 
    <gd:givenName>Elizabeth</gd:givenName> 
    <gd:familyName>Bennet</gd:familyName> 
    <gd:fullName>Elizabeth Bennet</gd:fullName> 
    </gd:name> 
    <atom:content type="text">Notes</atom:content> 
    <gd:email rel="http://schemas.google.com/g/2005#work" primary="true" address="[email protected]" displayName="E. Bennet"/> 
    <gd:email rel="http://schemas.google.com/g/2005#home" address="[email protected]"/> 
    </atom:entry>'; 
    
    $request = new \GuzzleHttp\Psr7\Request(
        'POST', 
        'https://www.google.com/m8/feeds/contacts/default/full', 
        ['Content-Type' => 'application/atom+xml; charset=UTF-8; type=entry'], 
        $xml 
    ); 
    
    $response = $httpClient->send($request);