不好的请求放置api Go

时间:2016-11-29 22:27:33

标签: go google-places-api

我使用google places api检索与搜索相关的所有结果,所以这就是我正在做的事情

type Places struct {
    ApiKey string
    Cx string
    c *maps.Client
}

func NewPlacesSearch(apiKey, cx string ) (*Places,error) {
    c, err := maps.NewClient(maps.WithAPIKey(apiKey))
    if err != nil {
        return nil, err
    }
    p := &Places{ApiKey:apiKey, Cx: cx, c:c}
    return p,nil
}

// Search returns all the results respect a keywords
func (p *Places) Search(search string) ([]*maps.PlacesSearchResponse, error) {
    results := make([]*maps.PlacesSearchResponse,0)
    search = url.QueryEscape(search)
    response, err := p.makeSearch(search, "")
    if err != nil {
        return results, err
    }
    results = append(results,response)
    fmt.Printf("%+v\n",response)
    done:= false
    if response.NextPageToken == "" {
        done = true
    }
    for !done {
        response, err = p.makeSearch(search, response.NextPageToken)
        if err != nil {
            log.Fatalf("fatal error: %s", err)
        }
        fmt.Println(response)
        if response.NextPageToken == "" {
            done = true
        }
        results = append(results,response)
    }
    return results, nil
}

func (p *Places) makeSearch(search, token  string) (*maps.PlacesSearchResponse,error) {
    r := &maps.TextSearchRequest{Query: search ,PageToken:token}
    fmt.Printf("%+v\n",r)
    response, err:= p.c.TextSearch(context.Background(),r)
    if err != nil {
        return  &maps.PlacesSearchResponse{}, err
    }
    return &response,nil
}

我试图从Google结果中迭代所有分页,正如您在Search函数内的第一个请求中看到的那样

response, err := p.makeSearch(search, "")

请求是

{
    Query:my+text+to+search
    Location:<nil> 
    Radius:0 
    Language: 
    MinPrice: 
    MaxPrice: 
    OpenNow:false 
    Type: 
    PageToken:
 }

但是当我尝试做第二个请求(在循环内部)时,我收到一条消息

fatal error: maps: INVALID_REQUEST - 

第二个请求如下:

{
    Query:my+text+to+search
    Location:<nil> 
    Radius:0 
    Language: 
    MinPrice: 
    MaxPrice: 
    OpenNow: false 
    Type: 
    PageToken: CuQB3AAAAMy72z91....
}

我做错了什么?

0 个答案:

没有答案